android:ToolBar詳解(手把手教程)
基礎使用
分成下列三個部分:
風格(style)
界面(layout)
程序(java)
風格(style)
風格要調整的地方有二
一在res/values/styles.xml中
二在res/values-v21/styles.xml中
因為此范例只使用Toolbar,所以我們要將原本的ActionBar隱藏起來,為方便,在/res/values/styles.xml增加一個名為AppTheme.Base的風格。然后將原本APPTheme的parent屬性改為AppTheme.Base.
<resources>
<!-- Base application theme. -->
<style name = "AppTheme" parent ="AppTheme.Base">
</style>
<style name = "AppTheme.Base" parent = "Theme.AppCompat">
<item name = "windowActionBar">false</item>
<del><item name="android:windowNoTitle">true</item></del>
<!-- 使用 API Level 22 編譯的話馍驯,要拿掉前綴字 -->
<item name = "windowNoTitle">true</item>
</style>
再來調整Android 5.0的style: /res/values-v21/styles.xml,也將其parent屬性改為AppTheme.Base:
<?xml version = "1.0" encoding ="utf-8"?>
<resources>
<style name = "AppTheme" parent = "AppTheme.Base">
</style>
</resources>
界面(Layout)
在activity_main.xml里面添加Toolbar控件:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent" >
</android.support.v7.widget.Toolbar>
這里需要注意,要將RelativeLayout里的四個方向的padding屬性去掉绅喉,并記得將原本的Hello World設為layout_below = "@+id/toolbar"
程序(java)
在MainActivity.java中加入Toolbar的聲明
Toolbar toolbar = (ToolBar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
聲明之后,再將之用setSupportActionBar()設定叫乌,Toolbar即能取代原本的actionbar了柴罐。
自定義顏色(Customization color)
- ColorPrimary(狀態(tài)欄顏色):在風格(styles)或是主題(themes)里進行設定。
- App bar底色
這個設定分為二憨奸,若你的 android app 仍是使用 actionbar 革屠,則直接在風格 (styles) 或是主題 (themes) 里進行設定 colorPrimary 參數(shù)即可;可若是采用 toolbar 的話排宰,則要在界面 (layout) 里面設定 toolbar 控件的 background 屬性似芝。 - navigationBarColor(導航欄底色)
僅能在 API v21 也就是 Android 5 以后的版本中使用, 因此要將之設定在 res/values-v21/styles.xml 里面
a)主視窗底色:windowBackground
我們需要設定的地方有三板甘,一是style中(/res/values/styles.xml)
<style name = "AppTheme.Base" parent = "Theme.AppCompat">
<item name = "windowActionBar">false</item>
<item name = "android:windowNoTitle">true</item>
<!--Actionbar color -->
<item name = "colorPrimary">@color/accent_material_dark</item>
<!--Status bar color -->
<item name = "colorPrimaryDark">@color/accent_material_light</item>
<!-- window color-->
<item name = "android:windowBackground">@color/dim_foreground_materi al_dark</item>
</style>
再來是v21的style中的(res/values-v21/styles.xml)
<style name="AppTheme" parent="AppTheme.Base">
<!--Navigation bar color-->
<item name="android:navigationBarColor">@color/accent_material_light</item>
</style>
最后党瓮,就是Toolbar的background進行設定
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary" >
</android.support.v7.widget.Toolbar>
在本范例中,Toolbar是設定來在activity_main.xml盐类,對其設定background屬性:android:background="?attr/colorPrimary"寞奸,這樣就可以使之沿用ActionBar的顏色設定嘍。
控件(component)
在<android.support.v7.widget.Toolbar>標簽中在跳,還有幾個大家常用的元素可以使用枪萄,如下圖:
- setNavigation
即設定up botton的圖標,因為Material的界面猫妙,在Toolbar這里的up botton樣式也有別于過去的ActionBar瓷翻。 - setLogo
APP的圖標 - setTitle
主標題 - setSubtitle
副標題 - setOnMenuItemClickListener
設定菜單各按鈕的動作
在MainActivity.java中的代碼:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// App Logo
toolbar.setLogo(R.drawable.ic_launcher);
// Title
toolbar.setTitle("My Title");
// Sub Title
toolbar.setSubtitle("Sub title");
setSupportActionBar(toolbar);
// Navigation Icon 要設定在 setSupoortActionBar 才有作用
// 否則會出現(xiàn) back button
toolbar.setNavigationIcon(R.drawable.ab_android);
這里需要留意的是setNavigationIcon需要放在setSupportActionBar之后才會生效。
菜單部分割坠,需要現(xiàn)在res/menu/menu_main.xml左定義:
<menu 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"
tools:context=".MainActivity">
<item android:id="@+id/action_edit"
android:title="@string/action_edit"
android:orderInCategory="80"
android:icon="@drawable/ab_edit"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_share"
android:title="@string/action_edit"
android:orderInCategory="90"
android:icon="@drawable/ab_share"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
再回到MainActivity.java中加入OnMenuItemClickListener的監(jiān)聽者:
private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
String msg = "";
swtich(menuItem.getItemId()) {
case R.id.action_edit:
msg +="Click edit";
break;
case R.id.action_share:
msg += "Click share";
break;
case R.id.action_settings:
msg +="CLick settting";
break;
}
if(!msg.equals("")) {
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
return ture;
}
};
將onMenuItemClick監(jiān)聽者設置給toolbar
setSupportActionBar(toolbar);
...
//Menu item click 的監(jiān)聽事件一樣要設定在 setSupportActionBar 才有作用
toolbar.setOnMenuItemClickListener(onMenuItemClick);
補充資料
android:layout_above="@id/xxx" --將控件置于給定ID控件之上
android:layout_below="@id/xxx" --將控件置于給定ID控件之下
android:layout_toLeftOf="@id/xxx" --將控件的右邊緣和給定ID控件的左邊緣對齊
android:layout_toRightOf="@id/xxx" --將控件的左邊緣和給定ID控件的右邊緣對齊
android:layout_alignLeft="@id/xxx" --將控件的左邊緣和給定ID控件的左邊緣對齊
android:layout_alignTop="@id/xxx" --將控件的上邊緣和給定ID控件的上邊緣對齊android:layout_alignRight="@id/xxx" --將控件的右邊緣和給定ID控件的右邊緣對齊android:layout_alignBottom="@id/xxx" --將控件的底邊緣和給定ID控件的底邊緣對齊android:layout_alignParentLeft="true" --將控件的左邊緣和父控件的左邊緣對齊
android:layout_alignParentTop="true" --將控件的上邊緣和父控件的上邊緣對齊
android:layout_alignParentRight="true" --將控件的右邊緣和父控件的右邊緣對齊android:layout_alignParentBottom="true" --將控件的底邊緣和父控件的底邊緣對齊android:layout_centerInParent="true" --將控件置于父控件的中心位置
android:layout_centerHorizontal="true" --將控件置于水平方向的中心位置
android:layout_centerVertical="true" --將控件置于垂直方向的中心位置