ToolBar教程:AppCompatActivity下用toolbar当actionbar用
参考:
https://developer.android.com/training/appbar/index.html
1,AppCompatActivity与常见的activity不同,要用 Theme.AppCompat.xxx系列中的一个不带actionbar的主题 。并可以自定义它。
1 <resources> 2 3 <!-- Base application theme. --> 4 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 5 6 <!--设置每个菜单项及它选中时的背景色--> 7 <item name="android:itemBackground">#a4f5f6</item> 8 <!--<item name="android:background">#303F9F</item>--> 9 10 <!--下面3行是ToolBar的状态栏沉浸效果--> 11 <!--<item name="android:windowTranslucentStatus">false</item>--> 12 <!--<item name="android:windowTranslucentNavigation">true</item>--> 13 <!--<item name="android:statusBarColor">#0176da</item>--> 14 15 <!-- Toolbar风格 --> 16 <item name="toolbarStyle">@style/Theme.Toolbar.App.Style</item> 17 18 <!--溢出菜单中的action间的分割线--> 19 <item name="android:dividerHeight">1dp</item> 20 <!--文本大小--> 21 <item name="android:textSize">20sp</item> 22 <!--文本颜色--> 23 <item name="android:textColorPrimary">#ffffff</item> 24 <!--设置菜单不遮挡actionbar--> 25 <item name="actionOverflowMenuStyle">@style/OverflowMenuStyle</item> 26 <!--配置Menu的图标--> 27 <item name="actionOverflowButtonStyle">@style/ToolBar.ActionButton.Overflow</item> 28 </style> 29 30 31 <!--溢出菜单样式 --> 32 <style name="OverflowMenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow"> 33 <!--溢出菜单的宽度--> 34 <item name="android:dropDownWidth">wrap_content</item> 35 <item name="android:paddingRight">5dp</item> 36 37 <!--溢出菜单窗口的背景,它与菜单项的背景不同,如果所有菜单项加一起盖住了窗口,那么这个背景色看不到--> 38 <item name="android:popupBackground">#89a81d</item> 39 40 <!--溢出菜单不遮挡ToolBar--> 41 <item name="overlapAnchor">false</item> 42 43 <!--溢出菜单垂直偏移,水平偏移--> 44 <item name="android:dropDownVerticalOffset">5dip</item> 45 <item name="android:dropDownHorizontalOffset">0dip</item> 46 </style> 47 48 <!--溢出按钮样式 --> 49 <style name="ToolBar.ActionButton.Overflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow"> 50 <item name="android:src">@android:drawable/ic_menu_add</item> 51 </style> 52 53 54 <!--=============下面是toolbar相关主题============--> 55 <!-- Toolbar样式 --> 56 <style name="Theme.Toolbar.App.Style" parent="@style/Widget.AppCompat.Toolbar"> 57 <item name="android:background">#33B5E5</item> 58 <!--注意不要用android:titleTextAppearance,要用xmlns:app="http://schemas.android.com/apk/res-auto"下的, 59 不带前缀就是app:下的--> 60 <!--主标题--> 61 <item name="title">title</item> 62 <!--副标题--> 63 <item name="subtitle">subtitle</item> 64 65 <!--logo--> 66 <item name="logo">@android:drawable/ic_menu_call</item> 67 <!--主标题样式--> 68 <item name="titleTextAppearance">@style/Theme.ToolBar.App.Title</item> 69 <!--副标题样式--> 70 <item name="subtitleTextAppearance">@style/Theme.ToolBar.App.Subtitle</item> 71 72 <item name="android:minHeight">?attr/actionBarSize</item> 73 <item name="titleMargins">4dp</item> 74 <!--ToolBar的Home图标左侧间隙--> 75 <item name="contentInsetStart">16dp</item> 76 77 <!--当home图标折叠时显示的图标--> 78 <item name="collapseIcon">@android:drawable/ic_menu_compass</item> 79 <item name="collapseContentDescription">collapseContentDescription</item> 80 <item name="android:paddingLeft">10dp</item> 81 <item name="android:paddingRight">10dp</item> 82 83 </style> 84 85 <!--设置toolbar标题主题--> 86 <style name="Theme.ToolBar.App.Title" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"> 87 <item name="android:textSize">20sp</item> 88 <item name="android:textColor">#ffffff</item> 89 </style> 90 91 <!--toolbar副标题主题--> 92 <style name="Theme.ToolBar.App.Subtitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle"> 93 <item name="android:textSize">9sp</item> 94 <item name="android:textColor">#ff0000</item> 95 </style> 96 </resources>
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 xmlns:app="http://schemas.android.com/apk/res-auto" 7 tools:context="com.txw.e.toolbardemo.MainActivity"> 8 9 <android.support.v7.widget.Toolbar 10 android:id="@+id/toolbar" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 /> 14 15 <!--app:contentInsetStart="3dp"--> 16 <!--app:title="Title"--> 17 <!--app:subtitle="subtitle"--> 18 <!--app:logo="@android:drawable/ic_menu_directions"--> 19 <!--app:titleTextAppearance="@style/Theme.ToolBar.Base.Title"--> 20 <!--app:subtitleTextAppearance="@style/Theme.ToolBar.Base.Subtitle"--> 21 22 <!--app:title等属性也可以定义在style.xml中,注意是app命名空间下的,不是android:title,后者不显示。 23 xmlns:app="http://schemas.android.com/apk/res-auto" 24 --> 25 26 <TextView 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:layout_below="@id/toolbar" 30 android:text="Hello toolbar!" /> 31 32 </RelativeLayout>
3,在layout对应的activity或fragment的onCreate中:
下面是activity示例
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_my); 5 Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 6 setSupportActionBar(myToolbar); 7 }
下面是fragment示例
1 public class MainView extends Fragment { 2 3 public Toolbar actionBar; 4 public void initActionBar(View rootView){ 5 //用toolbar实现 actionbar 6 actionBar = (Toolbar) rootView.findViewById(R.id.toolbar); 7 Drawable overflow = actionBar.getResources().getDrawable(R.drawable.ab_overflow_btn); 8 9 actionBar.setTitleTextColor(Color.WHITE); 10 actionBar.setOverflowIcon(overflow); 11 12 AppCompatActivity aca = (AppCompatActivity) getActivity(); 13 aca.setSupportActionBar(actionBar); 14 15 } 16 //... 17 }
4,添加action item,注意其中的 app:showAsAction 不是android:showAsAction 其它与actionbar类似。
1 <menu xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:tools="http://schemas.android.com/tools" 4 tools:context="com.e.weixin.MainActivity"> 5 6 <item 7 android:id="@+id/action_search" 8 android:icon="@android:drawable/ic_menu_search" 9 android:title="search" 10 app:actionViewClass="android.support.v7.widget.SearchView" 11 app:showAsAction="always|collapseActionView"/> 12 13 14 <item android:id="@+id/action_share" 15 android:icon="@android:drawable/ic_menu_share" 16 android:title="share" 17 app:showAsAction="ifRoom" 18 app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/> 19 <item 20 android:id="@+id/action_expand" 21 android:icon="@android:drawable/ic_menu_add" 22 android:title="展开/收起" 23 app:showAsAction="never|collapseActionView"/> 24 <item 25 android:id="@+id/action_home_as_up" 26 android:icon="@android:drawable/ic_menu_upload" 27 android:title="向上导航" 28 app:showAsAction="never"/> 29 <item 30 android:id="@+id/action_scan" 31 android:icon="@android:drawable/ic_menu_send" 32 android:title="scan" 33 app:showAsAction="never"/> 34 <item 35 android:id="@+id/action_get_money" 36 android:icon="@android:drawable/ic_menu_crop" 37 android:title="sq" 38 app:showAsAction="never"/> 39 <item 40 android:id="@+id/action_help_and_feedback" 41 android:icon="@android:drawable/ic_menu_help" 42 android:title="help" 43 app:showAsAction="never"/> 44 45 </menu>
1 @Override 2 public boolean onOptionsItemSelected(MenuItem item) { 3 switch (item.getItemId()) { 4 case R.id.action_settings: 5 // User chose the "Settings" item, show the app settings UI... 6 return true; 7 8 case R.id.action_favorite: 9 // User chose the "Favorite" action, mark the current item 10 // as a favorite... 11 return true; 12 13 default: 14 // If we got here, the user's action was not recognized. 15 // Invoke the superclass to handle it. 16 return super.onOptionsItemSelected(item); 17 18 } 19 }
6,处理向上导航事件,先设置activity上下级,然后得到ActionBar,然后用它处理向上导航事件。
1 <application ... > 2 ... 3 4 <!-- The main/home activity (it has no parent activity) --> 5 6 <activity 7 android:name="com.example.myfirstapp.MainActivity" ...> 8 ... 9 </activity> 10 11 <!-- A child of the main activity --> 12 <activity 13 android:name="com.example.myfirstapp.MyChildActivity" 14 android:label="@string/title_activity_child" 15 android:parentActivityName="com.example.myfirstapp.MainActivity" > 16 17 <!-- Parent activity meta-data to support 4.0 and lower --> 18 <meta-data 19 android:name="android.support.PARENT_ACTIVITY" 20 android:value="com.example.myfirstapp.MainActivity" /> 21 </activity> 22 </application>
处理代码
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_my_child); 5 6 // my_child_toolbar is defined in the layout file 7 Toolbar myChildToolbar = 8 (Toolbar) findViewById(R.id.my_child_toolbar); 9 setSupportActionBar(myChildToolbar); 10 11 // Get a support ActionBar corresponding to this toolbar 12 ActionBar ab = getSupportActionBar(); 13 14 // Enable the Up button 15 ab.setDisplayHomeAsUpEnabled(true); 16 }
在menu.xml对应有action项中
1 <item 2 android:id="@+id/action_search" 3 android:icon="@android:drawable/ic_menu_search" 4 android:title="search" 5 app:actionViewClass="android.support.v7.widget.SearchView" 6 app:showAsAction="always|collapseActionView"/>
在代码中
1 @Override 2 public boolean onCreateOptionsMenu(Menu menu) { 3 getMenuInflater().inflate(R.menu.main_activity_actions, menu); 4 5 MenuItem searchItem = menu.findItem(R.id.action_search); 6 SearchView searchView = 7 (SearchView) MenuItemCompat.getActionView(searchItem); 8 9 // Configure the search info and add any event listeners... 10 11 return super.onCreateOptionsMenu(menu); 12 }
8,响应action view 展开/收起 事件。要指定collapseActionView和app:actionViewClass。
1 <item 2 android:id="@+id/action_expand" 3 android:icon="@android:drawable/ic_menu_add" 4 android:title="展开/收起" 5 app:actionViewClass="android.support.v7.widget.SearchView" 6 app:showAsAction="always|collapseActionView"/>
指定了 collapseActionView
属性后,才可响应actionView的展开/收起事件。在按对应的item之前(action view展开之前),一直显示在
android:icon="xxx" 中指定的图标。
如果在activity中重写了onOptionsItemSelected() ,注意要调用 super.onOptionsItemSelected() 才能展开action view事件。
1 @Override 2 public boolean onCreateOptionsMenu(Menu menu) { 3 getMenuInflater().inflate(R.menu.options, menu); 4 // ... 5 6 // Define the listener 7 OnActionExpandListener expandListener = new OnActionExpandListener() { 8 @Override 9 public boolean onMenuItemActionCollapse(MenuItem item) { 10 // Do something when action item collapses 11 return true; // Return true to collapse action view 12 } 13 14 @Override 15 public boolean onMenuItemActionExpand(MenuItem item) { 16 // Do something when expanded 17 return true; // Return true to expand action view 18 } 19 }; 20 21 // Get the MenuItem for the action item 22 MenuItem actionMenuItem = menu.findItem(R.id.myActionItem); 23 24 // Assign the listener to that action item 25 MenuItemCompat.setOnActionExpandListener(actionMenuItem, expandListener); 26 27 // Any other things you have to do when creating the options menu… 28 29 return true; 30 }
1 <item android:id="@+id/action_share" 2 android:icon="@android:drawable/ic_menu_share" 3 android:title="share" 4 app:showAsAction="ifRoom" 5 app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
For information about creating a custom action provider, see the ActionProvider
reference. For information about configuring a ShareActionProvider
, see the reference for that class.