[Android]Toolbar

Toolbar是由AndroidX库提供的,它的强大之处在于,它不仅继承了ActionBar的所有功能,并且灵活度很高,可以配合其他控件完成一些Material Design的效果。

使用Toolbar替代ActionBar


在themes的两个xml文件中,

都指定一个不带ActionBar的主题

Theme.MaterialComponents.DayNight.NoActionBar:表示浅色主题,它会将界面的主题颜色设成浅色,陪衬颜色设为深色。

Theme.MaterialComponents.NoActionBar:表示深色主题,它会将界面的主题颜色设成深色,陪衬颜色设为浅色。

 修改activity_main.xml的代码

<FrameLayout
    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"
    tools:context=".MainActivity">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar1"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"//高度设置为actionBar的高度
                android:background="?attr/colorPrimary"//背景颜色设置为colorPrimary
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"//单独将弹出的菜单项指定为浅色主题
              />
/>

在MainActivity中添加

setSupportActionBar(toolbar)

Toolbar常用属性


在这里插入图片描述

<androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar2"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="主标题"
                app:subtitle="副标题"
                android:background="?attr/colorPrimary"
                app:logo="@drawable/ic_launcher_foreground"
                app:titleTextColor="@color/black"
                app:subtitleTextColor="@color/black"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                />

 添加菜单选项


res/menu/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/backup"//按钮的id
        android:icon="@drawable/is_backup"//指定按钮的图标
        android:title="Backup"//按钮的文字
        app:showAsAction="always"//指定按钮的显示位置
        />
    <item
        android:id="@+id/delect"
        android:icon="@drawable/is_delete"
        android:title="Delect"
        app:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/settings"
        android:icon="@drawable/is_settings"
        android:title="Settings"
        app:showAsAction="never"
        />

</menu>

 showAsAction的属性:

showAsAction
always永远显示在Toolbar中,如果屏幕空间不够则不显示
ifRoom表示屏幕空间足够的情况下显示在Toolbar中,不够的话就显示在菜单当中
never表示永远显示在菜单当中

注意:Toolbar中的action按钮只会显示图标,菜单中的action按钮只会显示文字。

设置菜单的点击事件


override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.toolbar,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.backup->Toast.makeText(this,"backup",Toast.LENGTH_SHORT).show()
            R.id.delect->Toast.makeText(this,"delect",Toast.LENGTH_SHORT).show()
            R.id.settings->Toast.makeText(this,"settings",Toast.LENGTH_SHORT).show()
        }
        return true
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值