深入理解Android 5.0中的Toolbar
環(huán)境說(shuō)明:
- Android Studio 2.0
- V7包版本:com.android.support:appcompat-v7:23.4.0
- compileSdkVersion 23
- buildToolsVersion "24.0.0"
Toolbar 引入使用
XML布局中加入:
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"/>
主題改為隱藏ActionBar:
Theme.AppCompat.Light.NoActionBar
Activity代碼中加入:
setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
此時(shí)運(yùn)行效果:
添加背景色
android:background="@color/colorPrimary"
此時(shí)運(yùn)行效果:
基本屬性設(shè)置
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" app:navigationIcon="@mipmap/title_bar_back"http://左側(cè)圖標(biāo) app:subtitle="子標(biāo)題" app:subtitleTextColor="#fff" //標(biāo)題顏色 app:title="標(biāo)題" app:titleTextColor="#fff"/> //子標(biāo)題顏色
運(yùn)行效果:
添加選項(xiàng)菜單
第一步創(chuàng)建菜單文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_settings" android:icon="@mipmap/ic_launcher" android:orderInCategory="100" android:title="settings" app:showAsAction="never"/> <item android:id="@+id/action_share" android:icon="@mipmap/ic_action_share" android:orderInCategory="100" android:title="settings" app:showAsAction="ifRoom"/> <item android:id="@+id/action_search" android:icon="@mipmap/ic_action_search" android:orderInCategory="100" android:title="settings" app:showAsAction="ifRoom"/> </menu>
第二部在代碼中重寫(xiě)onCreateOptionsMenu方法加載菜單文件
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; }
此時(shí)效果:
個(gè)性設(shè)置
左側(cè)返回箭頭
想要顯示自帶的返回箭頭,需要去掉之前設(shè)定的屬性:
app:navigationIcon="@mipmap/title_bar_back"
然后在代碼中添加:
getSupportActionBar().setHomeButtonEnabled(true); //設(shè)置返回鍵可用 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
此時(shí)效果:
溢出圖標(biāo)顏色
在style文件中添加:
<!-- 溢出菜單圖標(biāo)顏色--> <item name="colorControlNormal">@android:color/white</item>
此時(shí)效果:
自定義右側(cè)溢出圖標(biāo)
在Style文件中添加:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <!-- 溢出菜單圖標(biāo)顏色--> <item name="colorControlNormal">@android:color/white</item> <!-- 溢出菜單圖標(biāo)自定義--> <item name="android:actionOverflowButtonStyle">@style/OverflowButtonStyle</item> </style> <style name="OverflowButtonStyle" parent="android:Widget.ActionButton.Overflow"> <item name="android:src">@mipmap/ic_action_add</item> </style>
此時(shí)運(yùn)行效果:
更改彈出菜單背景
在Style文件中添加樣式:
在布局文件中添加使用主題:
app:popupTheme="@style/ToolbarPopupTheme"
此時(shí)運(yùn)行效果:
更改彈出菜單文字顏色
添加樣式文件:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <!-- 溢出菜單圖標(biāo)顏色--> <item name="colorControlNormal">@android:color/white</item> <item name="android:actionOverflowButtonStyle">@style/OverflowButtonStyle</item> <!-- 溢出菜單文字顏色--> <item name="textAppearanceLargePopupMenu">@style/Overflow_Menu_Text_style</item> </style> <!--溢出菜單文字顏色--> <style name="Overflow_Menu_Text_style" parent="@style/TextAppearance.AppCompat.Widget.PopupMenu.Large"> <item name="android:textColor">#fff</item> </style>
此時(shí)運(yùn)行效果:
修改標(biāo)題文字大小
添加配置:
app:titleTextAppearance="@style/ToolbarTitleSize"
添加style:
<!-- toolbar標(biāo)題文字大小 --> <style name="ToolbarTitleSize" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"> <item name="android:textSize">28sp</item> </style>
此時(shí)運(yùn)行效果:
子標(biāo)題文字大小類(lèi)似,添加配置然后定義style文件(此處省略):
app:subtitleTextAppearance="@style/ToolbarTitleSize"
修改彈出菜單位置
修改配置使彈出菜單顯示在Toolbar下方:
首先重新設(shè)置屬性:(在界面布局文件Toolbar中)
app:popupTheme="@style/OverflowMenuStyle"
在Style文件中添加:
<style name="OverflowMenuStyle"> <!-- 是否覆蓋錨點(diǎn),默認(rèn)為true,即蓋住Toolbar --> <item name="overlapAnchor">false</item> <item name="android:dropDownWidth">wrap_content</item> <item name="android:paddingRight">5dp</item> <!-- 彈出層背景顏色 --> <item name="android:colorBackground">#FFCC99</item> <!-- 彈出層垂直方向上的偏移,即在豎直方向上距離Toolbar的距離,值為負(fù)則會(huì)蓋住Toolbar --> <item name="android:dropDownVerticalOffset">5dp</item> <!-- 彈出層水平方向上的偏移,即距離屏幕左邊的距離,負(fù)值會(huì)導(dǎo)致右邊出現(xiàn)空隙 --> <item name="android:dropDownHorizontalOffset">0dp</item> <!-- 設(shè)置彈出菜單文字顏色 --> <item name="android:textColor">#0099CC</item> </style>
此時(shí)運(yùn)行效果:
事件處理
返回按鈕事件
添加監(jiān)聽(tīng)
toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "點(diǎn)擊了返回箭頭", Toast.LENGTH_LONG).show(); } });
菜單項(xiàng)點(diǎn)擊事件
重寫(xiě)方法
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: break; case R.id.action_search: break; case R.id.action_share: break; } return true; }
自定義Toolbar
Toolbar下面可以嵌套布局,直接將自己定義好的布局放到Toolbar下面即可
<android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> ...... </RelativeLayout> </android.support.v7.widget.Toolbar>
Toolbar 和 DrawerLayout 左滑菜單
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_left" android:layout_width="match_parent" android:layout_height="match_parent"> <!--側(cè)滑菜單--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="#CCCCFF" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="選項(xiàng)一" android:textSize="18sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="選項(xiàng)二" android:textSize="18sp"/> </LinearLayout> </android.support.v4.widget.DrawerLayout>
添加左滑布局文件:
在主布局文件中引入:(在Toolbar下方)
<!--DrawerLayout--> <include layout="@layout/custom_drawerlayout"/>
在代碼中添加關(guān)聯(lián):
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_left); ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close); mDrawerToggle.syncState(); mDrawerLayout.setDrawerListener(mDrawerToggle);
此時(shí)運(yùn)行效果:
新版本studio,在新建Activity的時(shí)候可以選擇對(duì)應(yīng)的模板,會(huì)自動(dòng)創(chuàng)建好DrawerLayout并關(guān)聯(lián)Toolbar.
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
- Android ToolBar控件詳解及實(shí)例
- Android Support Library 標(biāo)題欄(Toolbar)滾動(dòng)效果實(shí)現(xiàn)方法
- Android動(dòng)態(tài)修改ToolBar的Menu菜單示例
- Android折疊式Toolbar使用完全解析(CollapsingToolbarLayout)
- Android ToolBar整合實(shí)例使用方法詳解
- Android中Toolbar隨著ScrollView滑動(dòng)透明度漸變效果實(shí)現(xiàn)
- Android自定義Toolbar使用方法詳解
- Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn)
- Android5.0+ CollapsingToolbarLayout使用詳解
- ToolBar使用方法詳解
相關(guān)文章
Android開(kāi)發(fā)之圖片切割工具類(lèi)定義與用法示例
這篇文章主要介紹了Android開(kāi)發(fā)之圖片切割工具類(lèi)定義與用法,結(jié)合實(shí)例形式分析了Android圖片切割工具類(lèi)的定義與簡(jiǎn)單使用方法,需要的朋友可以參考下2017-11-11Android使用URLConnection提交請(qǐng)求的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android使用URLConnection提交請(qǐng)求的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10android使用ViewPager組件實(shí)現(xiàn)app引導(dǎo)查看頁(yè)面
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager組件實(shí)現(xiàn)app引導(dǎo)查看頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android ViewPager2 使用及自定義指示器視圖實(shí)現(xiàn)
這篇文章主要為大家介紹了Android ViewPager2 使用及自定義指示器視圖實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android仿人人網(wǎng)滑動(dòng)側(cè)邊欄效果
這篇文章主要為大家詳細(xì)介紹了Android仿人人網(wǎng)滑動(dòng)側(cè)邊欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android實(shí)現(xiàn)滑動(dòng)折疊Header全流程詳解
這篇文章主要介紹了Android實(shí)現(xiàn)滑動(dòng)折疊Header,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-11-11理解Android的手勢(shì)識(shí)別提高APP的用戶(hù)體驗(yàn)
對(duì)于觸摸屏,其原生的消息無(wú)非按下、抬起、移動(dòng)這幾種,我們只需要簡(jiǎn)單重載onTouch或者設(shè)置觸摸偵聽(tīng)器setOnTouchListener即可進(jìn)行處理2013-06-06Android安卓中循環(huán)錄像并檢測(cè)內(nèi)存卡容量
這篇文章主要介紹了Android安卓中循環(huán)錄像并檢測(cè)內(nèi)存卡容量,當(dāng)內(nèi)存卡空間已滿(mǎn)時(shí),本文還實(shí)現(xiàn)自動(dòng)刪除視頻列表里面的第一個(gè)文件,需要的朋友可以參考下2015-06-06