參考文章:【自定義控件】
系統原生標題欄叫做ActionBar
,而ToolBar
繼承了ActionBar
全部的功能,還有很高的靈活性。
一、原生標題欄
打開HelloWorld
項目的AndroidManifest.xml
文件,會發現android:theme
指定了屬性爲Apptheme
:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
......
繼續Ctrl+B
進去,找到styles.xml
文件:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
發現指定的主題是Theme.AppCompat.Light.DarkActionBar
深色主題,因爲要自定義標題欄,不能有原生標題欄,即Theme.AppCompat.NoActionBar
或Theme.AppCompat.Light.NoActionBar
。這裏我們選後者,淺色無標題欄。
其他參數指代的顏色:
colorPrimaryDark
:最上層狀態欄colorPrimary
:標題欄textColorPrimary
:標題欄文本windowBackground
:頁面背景colorAccent
:懸浮按鈕navigationBarColor
:導航欄
二、Toolbar
標題欄
這麼引入:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</FrameLayout>
- 第三行:
xmlns:app
指定命名空間。正是因爲xmlns:android
這麼寫,所以android:id
才能這麼寫。這裏指定了xmlns:app
,所以就能app:attribute
這麼寫了。因爲MD是Android 5.0
後的系統中出現的,很多MD屬性之前不存在,爲了兼容之前的系統,所以就用app:attribute
- 接着定義了
Toolbar
控件,它是appcompat
庫提供的- 我們指定它的高度爲
actionBar
的高度 - 它的背景色設置爲
colorPrimary
默認色
- 我們指定它的高度爲
- 剛纔在
styles.xml
中設置了程序主題爲淡色主題,因此ToolBar
也是淡色主題,所以ToolBar
上元素會自動變深色【白底黑字】,這樣很難看。所以ToolBar
需要單獨設置深色主題:android:theme
- 但是彈出的菜單項變成深色就很難看了,所以需要單獨設置淺色主題:
app:popupTheme
【兼容5.0-】
三、展示
修改主活動【導入類androidx.appcompat.widget.Toolbar
】:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
四、常用功能
1、修改標題欄文字
android:label
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="你好,呼吸君"
2、添加按鈕
在drawable-xxhdpi
文件夾下準備按鈕圖標,在res
下新建menu
目錄,在裏面新建Menu resource file
佈局toolbar.xml
:
<?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/mongo"
android:title="芒果"
android:icon="@drawable/mango"
app:showAsAction="always"/>
<item
android:id="@+id/cherry"
android:title="櫻桃"
android:icon="@drawable/cherry"
app:showAsAction="always"/>
<item
android:id="@+id/settings"
android:title="設置"
android:icon="@drawable/grape"
app:showAsAction="always"/>
</menu>
app:showAsAction
指定按鈕的顯示位置【ToolBar
顯示圖標,菜單
顯示文字】:
always
:永遠顯示,屏幕空間不夠則不顯示ifRoom
:屏幕控件夠則顯示,不夠就顯示在菜單裏never
:一直顯示在菜單中
修改活動:
// 加載`toolbar.xml`菜單
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
// 處理按鈕點擊事件
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.cherry:
Toast.makeText(this, "櫻桃🍒", Toast.LENGTH_SHORT).show();
break;
case R.id.mongo:
Toast.makeText(this, "芒果🍋", Toast.LENGTH_SHORT).show();
break;
case R.id.grape:
Toast.makeText(this, "葡萄🍇", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}