当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发——Toolbar常用设置

Android开发——Toolbar常用设置

2019年10月04日  | 移动技术网IT编程  | 我要评论

蝶尾园,韩德君女友,中国科学家或改写南海成因

本篇笔记用来记录常用的toolbar设置,如toolbar颜色设置,显示返回按钮,显示右边三个点按钮

之前android 使用的actionbar,android5.0开始,谷歌官方推荐使用toolbar来代替actionbar

最近慢慢开始使用上kotlin了,贴出的代码可能是kotlin的代码,见谅,如果有java基础的,其实还蛮简单上手的,可以参考一下我的kotlin学习笔记

kotlin学习笔记

1.使用toolbar替换actionbar

我们首先将主题设置为noactionbar,之后在布局xml文件添加toolbar

由android manifest文件进入theme,修改theme

<!-- base application theme. -->
<style name="apptheme" parent="theme.appcompat.light.noactionbar">
    <!-- customize your theme here. -->
    <item name="colorprimary">@color/colorprimary</item>
    <item name="colorprimarydark">@color/colorprimarydark</item>
    <item name="coloraccent">@color/coloraccent</item>
</style>

布局xml文件,添加toolbar

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout
    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:fitssystemwindows="true"
    android:layout_height="match_parent"
    tools:context="com.wan.noveldownloader.activity.mainactivity">
    <android.support.v7.widget.toolbar
        android:id="@+id/toolbar"
        app:titletextcolor="@color/white"
        android:background="@color/colorprimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</android.support.constraint.constraintlayout>

之后,在activity代码中,使用setsupporttoolbar,把toolbar设置进去

setcontentview(r.layout.activity_main);
//findviewbyid找到toolbar实例
setsupporttoolbar(toolbar);

之后运行就可以看到结果了

2.修改标题文字

默认的toolbar显示的文字其实就是你当前app项目的label,我们到androidmanifest文件修改activity的label属性,就可以达到修改文字的效果

上图中,我的app有两个activity,其中,mainactivity中的toolbar没有定义label属性,所以,默认label属性等于项目名,所有显示的是“星之小说下载器”

而另外的那个settingactivity则有label属性,所有,显示的文字就是“设置”

ps:如果不想要显示文字,则通过getsupportactionbar().setdisplayshowtitleenabled(false)实现(在setsupporttoolbar方法之后)

3.修改颜色

修改背景色

修改背景颜色通过修改toolbar的background属性达到效果

<android.support.v7.widget.toolbar
    android:id="@+id/toolbar"
    android:background="@color/colorprimary"
    android:layout_height="wrap_content"/>

修改标题文字颜色

修改titletextcolor属性,需要引入app命名空间

<android.support.v7.widget.toolbar
    android:id="@+id/toolbar"
    app:titletextcolor="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

4.显示左边返回按钮

通过代码的方式显示左边的返回按钮

setsupportactionbar(toolbar)
getsupportactionbar().sethomebuttonenabled(true)
getsupportactionbar().setdisplayhomeasupenabled(true)

activity中还需要重写onoptionsitemselected方法,点击返回按钮达到返回的效果

override fun onoptionsitemselected(item: menuitem?): boolean {
    if(item.itemid == android.r.id.home){
        finish()
    }
    return super.onoptionsitemselected(item)
}

5.显示toolbar的菜单按钮

1.创建menu.xml

在res目录下创建一个menu的文件夹,之后在menu文件夹中新建一个menu.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:title="设置" android:id="@+id/menu_setting" app:showasaction="always" android:icon="@drawable/icon_setting"/>
</menu>
  • title 标题
  • icon 图标
  • showasaction
    此属性有几个选择
    1. always:这个值会使菜单项一直显示在action bar上。
    2. ifroom:如果有足够的空间,这个值会使菜单项显示在action bar上。
    3. never:这个值使菜单项永远都不出现在action bar上。
    4. withtext:这个值使菜单项和它的图标,菜单文本一起显示。

2.重写oncreatemenu方法

重写activity中的oncreatemenu的方法,把menu.xml文件装载到app中

override fun oncreateoptionsmenu(menu: menu?): boolean {
    menuinflater.inflate(r.menu.menu,menu)
    return true
}

3.重写opoptionselect方法

设置每个菜单的点击事件,与设置监听器操作类似

override fun onoptionsitemselected(item: menuitem?): boolean {
    if (item?.itemid ==r.id.menu_setting) {
        startactivity(settingactivity::class.java)
    }
    return false
}

4.setsupporttoolbar

和之前的步骤一样

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网