Android 使用Toolbar實現(xiàn)應用欄實例詳解
使用Toolbar實現(xiàn)應用欄
App中應用欄是十分常見的,通常應用欄會顯示當前頁面的標題,還有一些操作按鈕,例如返回、搜索、掃碼等。本文介紹如何通過Toolbar實現(xiàn)應用欄。
使用Toolbar來實現(xiàn)應用欄,需要在AndroidManifest中設置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來設置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)
}
}
至此,一個簡單的應用欄已經(jīng)實現(xiàn)了,效果如圖:

應用欄功能擴展
返回
返回是應用欄中最常使用的功能,在Toolbar上使用返回功能,需要進行如下操作。
- 在
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及以下的設備-->
<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 {
// 可以自定義圖標的樣式
setHomeAsUpIndicator(R.drawable.icon_back)
setDisplayHomeAsUpEnabled(true)
}
}
}
效果如圖:

菜單
應用欄可能還會包含一些功能按鈕,例如搜索、掃一掃、打開設置頁面等,可以通過OptionsMenu快速實現(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)整菜單的樣式
由于我這邊對Toolbar的背景顏色進行了修改,需要調(diào)整OptionsMenu的圖標顏色和文字顏色來適配,如下:
<?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 {
// 如果需要在運行時對菜單進行調(diào)整(刪除或增加),在此處理
return super.onPrepareOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// 在此處理菜單項的點擊事件
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中查看,項目地址如下:
以上就是Android 使用Toolbar實現(xiàn)應用欄的詳細內(nèi)容,更多關(guān)于Android 使用Toolbar實現(xiàn)應用欄的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android下拉列表(Spinner)效果(使用C#和Java分別實現(xiàn))
這篇文章主要介紹了Android下拉列表(Spinner)效果(使用C#和Java分別實現(xiàn)),本文直接給出效果圖和兩種語言的實現(xiàn)代碼及布局代碼,需要的朋友可以參考下2015-06-06
Android 通過SQLite數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)存儲管理
SQLiteOpenHelper 是Android 提供的一個抽象工具類,負責管理數(shù)據(jù)庫的創(chuàng)建、升級工作。本文主要介紹了如何使用SQLite數(shù)據(jù)庫實現(xiàn)對數(shù)據(jù)進行存儲管理,感興趣的可以了解一下2021-11-11
Android ListView的item中嵌套ScrollView的解決辦法
有時候,listview 的item要顯示的字段比較多,考慮到顯示問題,item外面不得不嵌套ScrollView來實現(xiàn),糾結(jié)怎么解決此問題呢?下面小編給大家分享下Android ListView的item中嵌套ScrollView的解決辦法,感興趣的朋友一起看看吧2016-10-10
Android?Flutter中異常處理的方法總結(jié)
這篇文章主要為大家詳細介紹了Android?Flutter中異常處理的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起學習一下2023-06-06
解決Android從相冊中獲取圖片出錯圖片卻無法裁剪問題的方法
這篇文章主要介紹了解決Android從相冊中獲取圖片出錯圖片卻無法裁剪問題的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01

