Android development-Android Material Design Toolbar customization, androidtoolbar

Source: Internet
Author: User

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 =

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.