国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

深入理解Android 5.0中的Toolbar

2019-10-23 19:49:59
字體:
來源:轉載
供稿:網友

環境說明:

  • Android Studio 2.0
  • V7包版本:com.android/204327.html">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);

此時運行效果:

android,5.0,toolbar,android中toolbar

添加背景色

android:background="@color/colorPrimary"

此時運行效果:

android,5.0,toolbar,android中toolbar

基本屬性設置

<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"//左側圖標 app:subtitle="子標題" app:subtitleTextColor="#fff" //標題顏色 app:title="標題" app:titleTextColor="#fff"/> //子標題顏色

運行效果:

android,5.0,toolbar,android中toolbar

添加選項菜單

第一步創建菜單文件

 <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>

第二部在代碼中重寫onCreateOptionsMenu方法加載菜單文件

@Overridepublic boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true;}

此時效果:

android,5.0,toolbar,android中toolbar

個性設置

左側返回箭頭

想要顯示自帶的返回箭頭,需要去掉之前設定的屬性:

app:navigationIcon="@mipmap/title_bar_back"

然后在代碼中添加:

getSupportActionBar().setHomeButtonEnabled(true); //設置返回鍵可用getSupportActionBar().setDisplayHomeAsUpEnabled(true);

此時效果:

android,5.0,toolbar,android中toolbar

溢出圖標顏色

在style文件中添加:

 <!-- 溢出菜單圖標顏色--><item name="colorControlNormal">@android:color/white</item>

此時效果:

android,5.0,toolbar,android中toolbar

自定義右側溢出圖標

在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> <!-- 溢出菜單圖標顏色--> <item name="colorControlNormal">@android:color/white</item> <!-- 溢出菜單圖標自定義--> <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>

此時運行效果:

android,5.0,toolbar,android中toolbar

更改彈出菜單背景

在Style文件中添加樣式:

在布局文件中添加使用主題:

app:popupTheme="@style/ToolbarPopupTheme"

此時運行效果:

android,5.0,toolbar,android中toolbar

更改彈出菜單文字顏色

添加樣式文件:

<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>  <!-- 溢出菜單圖標顏色-->  <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>

此時運行效果:

android,5.0,toolbar,android中toolbar

修改標題文字大小

添加配置:

 app:titleTextAppearance="@style/ToolbarTitleSize"

添加style:

 <!-- toolbar標題文字大小 --> <style name="ToolbarTitleSize" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">  <item name="android:textSize">28sp</item> </style>

此時運行效果:

android,5.0,toolbar,android中toolbar

子標題文字大小類似,添加配置然后定義style文件(此處省略):

app:subtitleTextAppearance="@style/ToolbarTitleSize"

修改彈出菜單位置

修改配置使彈出菜單顯示在Toolbar下方:

首先重新設置屬性:(在界面布局文件Toolbar中)

app:popupTheme="@style/OverflowMenuStyle"

在Style文件中添加:

<style name="OverflowMenuStyle">  <!-- 是否覆蓋錨點,默認為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的距離,值為負則會蓋住Toolbar -->  <item name="android:dropDownVerticalOffset">5dp</item>  <!-- 彈出層水平方向上的偏移,即距離屏幕左邊的距離,負值會導致右邊出現空隙 -->  <item name="android:dropDownHorizontalOffset">0dp</item>  <!-- 設置彈出菜單文字顏色 -->  <item name="android:textColor">#0099CC</item> </style>

此時運行效果:

android,5.0,toolbar,android中toolbar

事件處理

返回按鈕事件

添加監聽

toolbar.setNavigationOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {    Toast.makeText(getApplicationContext(), "點擊了返回箭頭", Toast.LENGTH_LONG).show();   }  });

菜單項點擊事件

重寫方法

 @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"> <!--側滑菜單--> <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="選項一"   android:textSize="18sp"/>  <TextView   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:padding="10dp"   android:text="選項二"   android:textSize="18sp"/> </LinearLayout></android.support.v4.widget.DrawerLayout>

添加左滑布局文件:

在主布局文件中引入:(在Toolbar下方)

<!--DrawerLayout--><include layout="@layout/custom_drawerlayout"/>

在代碼中添加關聯:

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);

此時運行效果:

android,5.0,toolbar,android中toolbar

新版本studio,在新建Activity的時候可以選擇對應的模板,會自動創建好DrawerLayout并關聯Toolbar.

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 张家口市| 吕梁市| 新巴尔虎左旗| 德安县| 奉贤区| 红桥区| 山丹县| 聂荣县| 肇东市| 灵宝市| 若羌县| 台北县| 三都| 定边县| 昭平县| 儋州市| 曲阜市| 霍林郭勒市| 绥中县| 岳普湖县| 郸城县| 改则县| 漾濞| 焉耆| 兰坪| 阿尔山市| 石渠县| 伽师县| 卢氏县| 郓城县| 金川县| 揭西县| 化州市| 临邑县| 德钦县| 雷州市| 开阳县| 盘锦市| 贵港市| 登封市| 商南县|