Toolbar是Api21引入的一个控件,v7包含有它的控件,可兼容5.0以下系统,它是用来替代Actionbar,官方推荐Toolbar,它作为一个控件,比Actionbar更灵活也更强大。
官方文档描述:
Toolbar的组成分为5部分,分别是 A navigation button , A branded logo image , A title and subtitle , One or more custom views , An action menu .
Toolbar的使用:
0.修改App使用的主题,使用没有Actionbar的主题,否则会出错。
1 <resources> 2 3 <!-- Base application theme. --> 4 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 5 <!-- Customize your theme here. --> 6 <item name="colorPrimary">@color/colorPrimary</item> 7 <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 8 <item name="colorAccent">@color/colorAccent</item> 9 </style> 10 11 </resources>
1.在布局文件中使用控件,注意为了兼容低版本,使用android.support.v7.widget.Toolbar.
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:id="@+id/container" 7 tools:context="com.aellenlei.toolbardemo.MainActivity"> 8 9 10 <android.support.v7.widget.Toolbar 11 android:id="@+id/toolbar" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:background="#ff0004"/> 15 16 17 <TextView 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="Hello World" 21 android:layout_below="@+id/toolbar"/> 22 </RelativeLayout>
2.获取Toolbar对象,设置Toolbar的相关属性,
3.将Toolbar添加到Actionbar位置
eg:
1 <?xml version="1.0" encoding="utf-8"?> 2 <menu xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto"> 4 5 <item android:id="@+id/action_edit" 6 android:title="编辑" 7 android:orderInCategory="80" 8 android:icon="@android:drawable/ic_menu_edit" 9 app:showAsAction="ifRoom" /> 10 11 <item android:id="@+id/action_share" 12 android:title="分享" 13 android:orderInCategory="90" 14 android:icon="@android:drawable/ic_menu_share" 15 app:showAsAction="ifRoom" /> 16 17 <item android:id="@+id/action_settings" 18 android:title="settings" 19 android:orderInCategory="100" 20 app:showAsAction="never"/> 21 22 </menu> Toolbar的Items菜单项
1 package com.aellenlei.toolbardemo; 2 3 import android.os.Bundle; 4 import android.support.design.widget.Snackbar; 5 import android.support.v7.app.AppCompatActivity; 6 import android.support.v7.widget.Toolbar; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 10 public class MainActivity extends AppCompatActivity { 11 12 private Toolbar.OnMenuItemClickListener onMenuItemClickListener = new Toolbar.OnMenuItemClickListener() { 13 @Override 14 public boolean onMenuItemClick(MenuItem item) { 15 16 switch (item.getItemId()) { 17 case R.id.action_edit: 18 Snackbar.make(findViewById(R.id.container), "edit", Snackbar.LENGTH_SHORT).show(); 19 break; 20 case R.id.action_share: 21 Snackbar.make(findViewById(R.id.container), "share", Snackbar.LENGTH_SHORT).show(); 22 break; 23 case R.id.action_settings: 24 Snackbar.make(findViewById(R.id.container), "setting", Snackbar.LENGTH_SHORT).show(); 25 break; 26 } 27 28 29 return true; 30 } 31 }; 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_main); 37 38 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 39 toolbar.setLogo(R.mipmap.ic_launcher); 40 toolbar.setTitle("title"); 41 toolbar.setSubtitle("subtitle"); 42 toolbar.setNavigationIcon(R.mipmap.ic_launcher); 43 44 setSupportActionBar(toolbar); 45 46 47 toolbar.setOnMenuItemClickListener(onMenuItemClickListener); 48 49 } 50 51 @Override 52 public boolean onCreateOptionsMenu(Menu menu) { 53 getMenuInflater().inflate(R.menu.main, menu); 54 return super.onCreateOptionsMenu(menu); 55 } 56 57 58 } 实例化Toolbar
效果: