日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android toolbar源码解析,深入理解Android 5.0中的Toolbar

發布時間:2025/3/12 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android toolbar源码解析,深入理解Android 5.0中的Toolbar 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

環境說明:

Android Studio 2.0

V7包版本:com.android.support:appcompat-v7:23.4.0

compileSdkVersion 23

buildToolsVersion "24.0.0"

Toolbar 引入使用

XML布局中加入:

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:background="@color/colorPrimary"

此時運行效果:

基本屬性設置

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"/> //子標題顏色

運行效果:

添加選項菜單

第一步創建菜單文件

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

tools:context=".MainActivity">

android:id="@+id/action_settings"

android:icon="@mipmap/ic_launcher"

android:orderInCategory="100"

android:title="settings"

app:showAsAction="never"/>

android:id="@+id/action_share"

android:icon="@mipmap/ic_action_share"

android:orderInCategory="100"

android:title="settings"

app:showAsAction="ifRoom"/>

android:id="@+id/action_search"

android:icon="@mipmap/ic_action_search"

android:orderInCategory="100"

android:title="settings"

app:showAsAction="ifRoom"/>

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

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

此時效果:

個性設置

左側返回箭頭

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

app:navigationIcon="@mipmap/title_bar_back"

然后在代碼中添加:

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

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

此時效果:

溢出圖標顏色

在style文件中添加:

@android:color/white

此時效果:

自定義右側溢出圖標

在Style文件中添加:

@color/colorPrimary

@color/colorPrimaryDark

@color/colorAccent

@android:color/white

@style/OverflowButtonStyle

@mipmap/ic_action_add

此時運行效果:

更改彈出菜單背景

在Style文件中添加樣式:

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

app:popupTheme="@style/ToolbarPopupTheme"

此時運行效果:

更改彈出菜單文字顏色

添加樣式文件:

@color/colorPrimary

@color/colorPrimaryDark

@color/colorAccent

@android:color/white

@style/OverflowButtonStyle

@style/Overflow_Menu_Text_style

#fff

此時運行效果:

修改標題文字大小

添加配置:

app:titleTextAppearance="@style/ToolbarTitleSize"

添加style:

28sp

此時運行效果:

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

app:subtitleTextAppearance="@style/ToolbarTitleSize"

修改彈出菜單位置

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

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

app:popupTheme="@style/OverflowMenuStyle"

在Style文件中添加:

false

wrap_content

5dp

#FFCC99

5dp

0dp

#0099CC

此時運行效果:

事件處理

返回按鈕事件

添加監聽

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:layout_width="match_parent"

android:layout_height="?attr/actionBarSize">

android:layout_width="match_parent"

android:layout_height="match_parent">

......

Toolbar 和 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">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="start"

android:background="#CCCCFF"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:text="選項一"

android:textSize="18sp"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:text="選項二"

android:textSize="18sp"/>

添加左滑布局文件:

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

在代碼中添加關聯:

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

此時運行效果:

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

總結

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

總結

以上是生活随笔為你收集整理的android toolbar源码解析,深入理解Android 5.0中的Toolbar的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。