Material Design是谷歌提出來的最新ui設計規范,我想actionbar大家也許並不陌生,toolbar簡單而言可以看做為actionbar的升級版,相比較actionbar而言,toolbar可以隨處放,顯得比較自由,下面我們來看一下如何使用toolbar:
在 Android studio的編譯環境中:
1:需要v7包的支持在build.gradle里面導入v7jar包
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 了
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mToolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//設置是否有返回箭頭
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
到這一步基本就可以出來效果了 如下圖:2代表的是,使用的是沉浸式狀態欄,下一篇會提到
1代表返回箭頭,如何讓toolbar 上顯示返回箭頭,有兩種方式,在對應actvity里面定義(建議這些操作放在baseActivity里面)
第一種:在Androidmanifest里面直接定義它的父activity,子activity就會顯示返回箭頭
<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); }