• Android Material Design之MaterialToolbar(三)


    • 上图
      在这里插入图片描述

    • 引入

    implementation 'com.google.android.material:material:1.4.0'
    
    • 1
    • 属性
    属性描述
    app:menu右侧菜单文件
    app:navigationIcon左侧返回按钮
    app:title主标题栏文本
    app:titleCentered主标题栏文本是否居中
    app:titleTextColor主标题栏文本颜色
    app:subtitle子标题栏文本
    app:subtitleCentered子标题栏是否居中
    app:subtitleTextColor子标题栏文本颜色
    app:logo标题栏logo图标
    • 使用
    1. 布局源代码
    
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/design_default_color_primary_variant"
            app:menu="@menu/menu"
            app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
            app:subtitle="我是一个子标题"
            app:subtitleCentered="false"
            app:subtitleTextColor="@color/white"
            app:title="我是一个标题"
            app:titleCentered="true"
            app:logo="@mipmap/ic_launcher"
            app:titleTextColor="@color/white" />
    androidx.coordinatorlayout.widget.CoordinatorLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. menu源代码
    
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        
        <item
            android:id="@+id/more"
            android:icon="@drawable/ic_baseline_more_vert_24"
            android:title="更多"
            app:showAsAction="always">
            <menu>
                <item
                    android:id="@+id/scan"
                    android:title="扫一扫" />
            menu>
        item>
    menu>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. activity 源代码
    package com.yyf.demo;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.Window;
    
    import com.yyf.demo.databinding.ActivityMain3Binding;
    
    public class MainActivity extends AppCompatActivity {
        private ActivityMain3Binding binding;
        private static final String TAG = "MainActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
            binding = ActivityMain3Binding.inflate(getLayoutInflater());
            setContentView(binding.getRoot());
            //引入toolBar
            setSupportActionBar(binding.toolbar);
    		//toolBar 左侧返回图标事件
            binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "onClick: 点击了返回图标");
                }
            });
        }
    	
    	//引入menu布局文件
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu, menu);
            return true;
        }
    	
    	//menu选项被点击响应事件
        @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.scan:
                    Log.d(TAG, "onOptionsItemSelected: " + item.getTitle());
                    break;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 注意事项

    java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
    该异常说明已经有一个标题栏存在,不能再去设置一个,解决方式如下:

    1. 在activity中setContentView()前设置supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    2. AndroidManifest.xml中修改application标签theme属性
    <style name="Theme.BaseTheme" parent="Theme.Design.NoActionBar">
    style>
    
    • 1
    • 2
    • 文中Activity使用了ViewBinding 若直接复制报错,请在项目中build.gradle android标签 下开启
    	buildFeatures{
            viewBinding=true
        }
    
    • 1
    • 2
    • 3
  • 相关阅读:
    YOLO目标检测——交通标志数据集+已标注voc和yolo格式标签下载分享
    2核4G服务器5M带宽?选腾讯云轻量应用服务器吧
    node-sass安装不上的问题
    多分类-手写识别体
    Git使用教程
    JAVA计算机毕业设计电视设备租借系统Mybatis+系统+数据库+调试部署
    【golang】error parsing regexp: invalid or unsupported Perl syntax (正则表达式校验密码)
    程序员下班为什么从来不关电脑?
    WorkPlus Meet白板和文档共享功能上线,私有化视频会议全新升级
    PMI-ACP练习题(24)
  • 原文地址:https://blog.csdn.net/csdn_yang123/article/details/128000515