Android development-Android Material Design Toolbar customization, androidtoolbar
1. menu of custom Toolbar:
Create a menu. xml file under menu to customize the menu style:
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=".MainActivity"> 5 <item 6 android:id="@+id/action_search" 7 android:orderInCategory="80" 8 android:title="action_search" 9 app:showAsAction="ifRoom"10 android:icon="@drawable/search_ic_selector"/>11 </menu>
2. Custom Toolbar, which is generally shared:
Create a common_toolbar.xml file:
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.v7.widget.Toolbar 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:id="@+id/common_toolbar_top" 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 android:background="@color/colorPrimary" 9 android:minHeight="?attr/actionBarSize"10 app:popupTheme="@style/ThemeOverlay.AppCompat.Light"11 app:navigationIcon="?attr/homeAsUpIndicator"12 app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"13 >14 </android.support.v7.widget.Toolbar>
Note: app: navigationIcon = "? Attr/homeAsUpIndicator "is used to set the return icon.
3. Introduce custom Toolbar in the layout file:
<include layout="@layout/common_toolbar"></include>
4. Declare Toolbar in activity and listen to menu events:
Note: The Activity must inherit the AppCompatActivity
1. Declare Toolbar:
1 Toolbar toolbar = (Toolbar) findViewById(R.id.common_toolbar_top);2 setSupportActionBar(toolbar);
2. Set the title of a Toolbar:
setTitle(R.string.fragment_for_why_title);
3. Declare the menu and listen for events:
Menu declaration:
@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}
Event listening:
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_search) { return true; } return super.onOptionsItemSelected(item);}
V. Final:
Demo: http://shouji.baidu.com/software/item? Docid = 8118536 & from =