Meterial Design - Floating Action Button
需要在build.gradle 加入compile 'com.android.support:design:25.3.1'
elevation 陰影高度
pressedTranslationZ按下的時候陰影就會增大
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
android:clickable="true"
app:backgroundTint="@color/purpleColorPrimary"
app:rippleColor="@color/colorAccent"
app:fabSize="normal"
app:srcCompat="@android:drawable/ic_input_add"
android:layout_gravity="bottom|end"/>
Meterial Design - Snackbar
Snack bar 跟 Toast 不同的是,Snack bar 是以 view 作參數,而不是以 contextSnackbar.make(contentView, "It is Snackbar!!!", Snackbar.LENGTH_SHORT).show();
Snackbar加上「行動」按鍵的話,可以使用setAction( );
Snackbar.make(textDrawerItemName,menuItem.getTitle() + "pressed",
Snackbar.LENGTH_SHORT).show();
Meterial Design-CoordinatorLayout
CoordinatorLayout 是 FrameLayout 的強化版,主要有以下兩個作用:1.作為頂層佈局
2.協調個子佈局之間的容器
當設計元件時,FAB放至右下角, Snackbar 時,會擋住FAB,這時後就要使CoordinatorLayoutayout_anchor:設置 FAB 的錨
layout_anchorGravity:設置相對錨點的位置,如bottom|right表示 FAB 位於錨點的右下角。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"/>
<LinearLayout
android:id="@+id/anchorView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textDrawerItemName"
android:layout_marginTop="?attr/actionBarSize"
android:padding="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:text="@string/drawer_item"
app:layout_anchor="@+id/toolbar"
app:layout_anchorGravity="bottom|center_horizontal"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
android:clickable="true"
app:backgroundTint="@color/purpleColorPrimary"
app:rippleColor="@color/colorAccent"
app:fabSize="normal"
app:srcCompat="@android:drawable/ic_input_add"
app:layout_anchor="@id/anchorView"
app:layout_anchorGravity="bottom|right"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ActionBarDrawerToggle drawerToggle; //在Toolbar上加上「三」圖示
TextView textDrawerItemName;
DrawerLayout drawerLayout;
NavigationView nav_View;
FloatingActionButton fab;
private static final String NAV_ITEM_ID = "nav_index";
//用來儲存點擊了的 menuItem id,必免旋轉螢幕後 menu 和 textDrawerItemName 會被重設
private int navItemId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//view
initView();
//Handler
initDrawerLayout();
initNavigationView(savedInstanceState);
initFab();
}
private void initView() {
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
textDrawerItemName = (TextView)findViewById(R.id.textDrawerItemName);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
nav_View = (NavigationView)findViewById(R.id.navigation_view);
fab = (FloatingActionButton)findViewById(R.id.fab);
}
private void initDrawerLayout() {
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,
R.string.open_drawer,R.string.close_drawer){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
}
private void initNavigationView(Bundle savedInstanceState ) {
nav_View.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
//Snack bar 跟 Toast 不同的是,Snack bar 是以 view 作參數,而不是以 context
Snackbar.make(textDrawerItemName,menuItem.getTitle() + "pressed",
Snackbar.LENGTH_SHORT).show();
navigateTo(menuItem);
drawerLayout.closeDrawers();
return true;
}
});
//利用Bundle 取出儲存的資料
if (null != savedInstanceState){
navItemId = savedInstanceState.getInt(NAV_ITEM_ID, R.id.navigation_item_1);
}else {
navItemId = R.id.navigation_item_1;
}
navigateTo(nav_View.getMenu().findItem(navItemId));
}
private void navigateTo(MenuItem menuItem) {
textDrawerItemName.setText(menuItem.getTitle());
//這裡會用 navItemId 記著當前的 menuItem。
navItemId = menuItem.getItemId();
menuItem.setCheckable(true);
}
//之後再用 onSaveInstanceState() 來記著當前的 navItemId
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//裝目前狀態值存儲起來
outState.putInt(NAV_ITEM_ID, navItemId);
}
private void initFab(){
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(textDrawerItemName, "Snack bar!!!!",Snackbar.LENGTH_SHORT)
.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("Undo pressed")
.setNeutralButton("OK", null)
.show();
}
}).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_settings:
Snackbar.make(textDrawerItemName,item.getTitle(),
Snackbar.LENGTH_SHORT).show();
return true;
default:
}
return super.onOptionsItemSelected(item);
}
}
留言
張貼留言