Material Design是谷歌提出來的最新ui設計規範,我想actionbar你們也許並不陌生,toolbar簡單而言能夠看作爲actionbar的升級版,相比較actionbar而言,toolbar能夠隨處放,顯得比較自由,下面咱們來看一下如何使用toolbar:android
在 Android studio的編譯環境中:app
1:須要v7包的支持在build.gradle裏面導入v7jar包ide
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' }
2.在style 裏面將主題裏面的action bar給屏蔽掉佈局
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- toolbar(actionbar)顏色 --> <item name="colorPrimary">#4876FF</item> <!-- 狀態欄顏色 --> <item name="colorPrimaryDark">#3A5FCD</item> <!-- 窗口的背景顏色 --> <item name="android:windowBackground">@android:color/white</item> </style>
3.在xml佈局中 定義toolbar,通常狀況,咱們都是單獨一個佈局,在其餘須要的佈局中直接include 就ok了測試
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.toolbar" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.ActionBar" > android:fitsSystemWindows="true" </android.support.v7.widget.Toolbar>
4.在activity裏設置用 setSupportActionBar 設定,Toolbar即能取代本來的 actionbar 了gradle
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mToolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//設置是否有返回箭頭
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
到這一步基本就能夠出來效果了 以下圖:2表明的是,使用的是沉浸式狀態欄,下一篇會提到ui
1表明返回箭頭,如何讓toolbar 上顯示返回箭頭,有兩種方式,在對應actvity裏面定義(建議這些操做放在baseActivity裏面) this
第一種:在Androidmanifest裏面直接定義它的父activity,子activity就會顯示返回箭頭spa
<activity android:name=".secondActivity" android:label="@string/title_activity_second" android:parentActivityName=".MainActivity"> <!-- Parent activity meta-data to support 4.0 and lower --> 目前支持4.0以上的,meta-data是爲了讓低版本也支持 <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity>
第二種:代碼設置,有的時候咱們會出現一種佈局多用的狀況,這時候,他的父activity就不僅一個了,這個時候,咱們就不能像第一種那樣設置 了,咱們須要在代碼裏面設置設計
在父類activity裏面設置:
Intent intent= new Intent(this); intent.putString("parent_activity_name",getClass().getSimpleName());getClass().getSimpleName()獲取當前類名 tostarActivityf(SuccessfulActivity.class, intent);
在子activity裏面複寫getSupportParentActivityIntent()方法
@Override public Intent getSupportParentActivityIntent() { Intent parentIntent = getIntent(); String className = parentIntent.getStringExtra("parent_activity_name"); Intent newIntent = null; try { //you need to define the class with package name newIntent = new Intent(SuccessfulActivity.this, Class.forName(getPackageName() + className)); } catch (ClassNotFoundException e) { e.printStackTrace(); } return newIntent; }
我本身測試了一下,感受這個方法好像沒有調用,也許是我在baseActivity裏面重寫如下方法,將他直接finish,你們能夠研究一下。
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { //重寫ToolBar返回按鈕的行爲,防止從新打開父Activity重走生命週期方法 case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); }