参考文章:【自定义控件】 系统原生标题栏叫做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.NoActionBarTheme.AppCompat.Light.NoActionBar。这里我们选后者,浅色无标题栏。

其他参数指代的颜色:

  1. colorPrimaryDark :最上层状态栏
  2. colorPrimary:标题栏
  3. textColorPrimary:标题栏文本
  4. windowBackground:页面背景
  5. colorAccent:悬浮按钮
  6. 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);
    }
}

materialDesign设置 DatePicker 背景色 material theme ui怎么设置_安卓

四、常用功能

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、展示

materialDesign设置 DatePicker 背景色 material theme ui怎么设置_标题栏_02