参考文章:【自定义控件】 系统原生标题栏叫做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;
}
3、展示