Android 使用Toolbar實(shí)現(xiàn)應(yīng)用欄實(shí)例詳解
使用Toolbar實(shí)現(xiàn)應(yīng)用欄
App中應(yīng)用欄是十分常見的,通常應(yīng)用欄會(huì)顯示當(dāng)前頁面的標(biāo)題,還有一些操作按鈕,例如返回、搜索、掃碼等。本文介紹如何通過Toolbar實(shí)現(xiàn)應(yīng)用欄。
使用Toolbar來實(shí)現(xiàn)應(yīng)用欄,需要在AndroidManifest中設(shè)置NoActionBar的主題,并且Activity需要繼承AppCompatActivity。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:theme="Theme.MaterialComponents.DayNight.NoActionBar">
...
</application>
</manifest>
class ToolbarActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
在布局文件中添加Toolbar控件,如下:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color_23242a"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="@color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
在Activiy的onCreate方法中使用setSupportActionBar來設(shè)置Toolbar,代碼如下:
class ToolbarActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: LayoutToolbarActivityBinding = DataBindingUtil.setContentView(this, R.layout.layout_toolbar_activity)
setSupportActionBar(binding.toolbar)
}
}
至此,一個(gè)簡(jiǎn)單的應(yīng)用欄已經(jīng)實(shí)現(xiàn)了,效果如圖:

應(yīng)用欄功能擴(kuò)展
返回
返回是應(yīng)用欄中最常使用的功能,在Toolbar上使用返回功能,需要進(jìn)行如下操作。
- 在
AndroidManifest中配置父Activity,如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:theme="Theme.MaterialComponents.DayNight.NoActionBar">
...
<activity
android:name="com.chenyihong.exampledemo.toolbar.ToolbarActivity"
android:parentActivityName="com.chenyihong.exampledemo.home.HomeActivity"
android:screenOrientation="portrait">
<!--適配 Android 4.0及以下的設(shè)備-->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.chenyihong.exampledemo.home.HomeActivity" />
</activity>
</application>
</manifest>
- 在
Activiy的onCreate方法中使用setDisplayHomeAsUpEnabled來顯示返回按鈕,代碼如下:
class ToolbarActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: LayoutToolbarActivityBinding = DataBindingUtil.setContentView(this, R.layout.layout_toolbar_activity)
setSupportActionBar(binding.toolbar)
supportActionBar?.run {
// 可以自定義圖標(biāo)的樣式
setHomeAsUpIndicator(R.drawable.icon_back)
setDisplayHomeAsUpEnabled(true)
}
}
}
效果如圖:

菜單
應(yīng)用欄可能還會(huì)包含一些功能按鈕,例如搜索、掃一掃、打開設(shè)置頁面等,可以通過OptionsMenu快速實(shí)現(xiàn)。
- 在res/menu目錄下創(chuàng)建
Menu Resource File,如下
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/icon_search"
android:title="Search"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_scan"
android:icon="@drawable/icon_scan"
android:title="Scan"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_setting"
android:icon="@drawable/icon_setting"
android:title="Setting"
app:showAsAction="never" />
</menu>
- 調(diào)整菜單的樣式
由于我這邊對(duì)Toolbar的背景顏色進(jìn)行了修改,需要調(diào)整OptionsMenu的圖標(biāo)顏色和文字顏色來適配,如下:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--android:theme用于指定Toolbar的樣式-->
<!--app:popupTheme用于指定Menu的樣式-->
<androidx.appcompat.widget.Toolbar
...
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.DayNight.ActionBar"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
- 在
Activity中配置菜單
class ToolbarActivity : AppCompatActivity() {
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.example_menu, menu)
return true
}
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
// 如果需要在運(yùn)行時(shí)對(duì)菜單進(jìn)行調(diào)整(刪除或增加),在此處理
return super.onPrepareOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// 在此處理菜單項(xiàng)的點(diǎn)擊事件
when (item.itemId) {
R.id.action_search -> {
showToast("click search menu")
}
R.id.action_scan -> {
showToast("click scan menu")
}
R.id.action_setting -> {
showToast("click setting menu")
}
}
return super.onOptionsItemSelected(item)
}
private fun showToast(message: String) {
runOnUiThread { Toast.makeText(this, message, Toast.LENGTH_SHORT).show() }
}
...
}
效果如圖:

示例
完整示例代碼可以在demo中查看,項(xiàng)目地址如下:
以上就是Android 使用Toolbar實(shí)現(xiàn)應(yīng)用欄的詳細(xì)內(nèi)容,更多關(guān)于Android 使用Toolbar實(shí)現(xiàn)應(yīng)用欄的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android下拉列表(Spinner)效果(使用C#和Java分別實(shí)現(xiàn))
這篇文章主要介紹了Android下拉列表(Spinner)效果(使用C#和Java分別實(shí)現(xiàn)),本文直接給出效果圖和兩種語言的實(shí)現(xiàn)代碼及布局代碼,需要的朋友可以參考下2015-06-06
Android 通過SQLite數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理
SQLiteOpenHelper 是Android 提供的一個(gè)抽象工具類,負(fù)責(zé)管理數(shù)據(jù)庫的創(chuàng)建、升級(jí)工作。本文主要介紹了如何使用SQLite數(shù)據(jù)庫實(shí)現(xiàn)對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ)管理,感興趣的可以了解一下2021-11-11
Android ListView的item中嵌套ScrollView的解決辦法
有時(shí)候,listview 的item要顯示的字段比較多,考慮到顯示問題,item外面不得不嵌套ScrollView來實(shí)現(xiàn),糾結(jié)怎么解決此問題呢?下面小編給大家分享下Android ListView的item中嵌套ScrollView的解決辦法,感興趣的朋友一起看看吧2016-10-10
android動(dòng)態(tài)壁紙調(diào)用的簡(jiǎn)單實(shí)例
動(dòng)態(tài)壁紙的實(shí)現(xiàn)其實(shí)就是在Activity中調(diào)用動(dòng)態(tài)壁紙服務(wù),通過綁定服務(wù)得到IWallpaperService,調(diào)用該接口中的attach函數(shù)實(shí)現(xiàn)壁紙的調(diào)用。2013-06-06
Flutter實(shí)現(xiàn)倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android?Flutter中異常處理的方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Android?Flutter中異常處理的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06
解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法
這篇文章主要介紹了解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01

