Android开发之Android Material Design Toolbar自定义随笔
生活随笔
收集整理的這篇文章主要介紹了
Android开发之Android Material Design Toolbar自定义随笔
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、自定義Toolbar的menu:
在menu下新建menu.xml文件,自定義menu的樣式:
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>二、自定義Toolbar,Toolbar一般是共用:
新建common_toolbar.xml文件:
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>注:其中app:navigationIcon="?attr/homeAsUpIndicator"用于設(shè)置返回圖標(biāo)
三、在布局文件中引入自定義的Toolbar:
<includelayout="@layout/common_toolbar"> </include>四、activity中聲明Toolbar以及對menu的事件監(jiān)聽:
注:Activity必須繼承AppCompatActivity1、聲明Toolbar:
1 Toolbar toolbar = (Toolbar) findViewById(R.id.common_toolbar_top); 2 setSupportActionBar(toolbar);2、Toolbar設(shè)置標(biāo)題等:
setTitle(R.string.fragment_for_why_title);3、對menu進(jìn)行聲明和事件監(jiān)聽:
menu聲明:
@Override public 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; }事件監(jiān)聽:
@Override public boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();//noinspection SimplifiableIfStatementif (id == R.id.action_search) {return true;}return super.onOptionsItemSelected(item); }五、最終效果圖:
? ? ? ? ? ? ? ? ? ? ??
Demo下載地址:http://shouji.baidu.com/software/item?docid=8118536&from=as
?
轉(zhuǎn)載于:https://www.cnblogs.com/LT5505/p/4974237.html
總結(jié)
以上是生活随笔為你收集整理的Android开发之Android Material Design Toolbar自定义随笔的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS 控制屏幕横竖屏旋转
- 下一篇: python代码自动补齐插件