android ToolBar 设置颜色

2018-10-30 00:16:41


通常情况加我们需要与toolbar标题统一的颜色,比如白色,那么需要在布局文件中的toolbar空间中再添加两行样式代码即可:

<android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        style="@style/Toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetStart="0dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        >

app:popupTheme——有时候我们有需求:ActionBar文字是白的,ActionBar Overflow弹出的是白底黑字让ActionBar文字是白的,那么对应的theme肯定是Dark。可是让ActionBar弹出的是白底黑字,那么需要Light主题。这时候popupTheme就派上用场了。android:theme 与app:theme——在AppCompat v21里,提供了一个快速方便的方法设置Toolbar的主题,使用app:theme。而新版本22.1.x中,AppCompat 允许对 Toolbar 使用 android:theme代替 app:theme。最好的一点是:它会自动继承父视图的theme ,并且兼容所有APIv11以上的设备。


例如下面


 <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
 
        <View
            android:layout_width="match_parent"
            android:layout_height="24dp"/>
 
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#ffF5F5F5"
            app:layout_scrollFlags="scroll"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/AppTheme.PopupOverlay">
 
            <TextView
                android:id="@+id/toolbar_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:singleLine="true"
                android:text="编辑"
                android:textColor="@color/left_title"
                android:textSize="18sp" />
 
 
        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:singleLine="true"
            android:text="自选"
            android:textColor="@color/background_title"
            android:textSize="20sp" />
 
        </android.support.v7.widget.Toolbar>
 
    </android.support.design.widget.AppBarLayout>

自定义主题文件如下


 <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
        <!-- 设置Menu菜单的背景色 -->
        <item name="android:itemBackground">@color/left_title</item>
        <!-- 设置Menu菜单的字体颜色 -->
        <item name="android:textColorPrimary">@color/left_title</item>
        <item name="colorControlNormal">@color/left_title</item><!-- 主要是这个起作用,修改默认返回键的颜色-->
        <!-- 设置Menu窗口不覆盖Toolbar视图 -->
        <item name="overlapAnchor">false</item>
    </style>


  • 2017-04-16 21:02:55

    ImageView的android:adjustViewBounds属性

    取值为true时: Adjust the ImageView's bounds to preserve the aspect ration of its drawable. 调整ImageView的界限来保持图像纵横比不变。 这并不意味着ImageView的纵横比就一定和图像的纵横比相同

  • 2017-04-18 17:12:50

    Laravel 读取 config 下的数据

    Laravel的config下一般存放配置信息,可以通过config('key')方法获取指定的数据。 设置值可通过「点」式语法读取,其中包含要访问的文件名以及选项名称。

  • 2017-04-26 16:43:03

    php对象和数组相互转换的方法

    这篇文章主要介绍了php对象和数组相互转换的方法,通过两个自定义函数实现对象与数组的相互转换功能,非常简单实用,需要的朋友可以参考下

  • 2017-04-26 22:59:15

    百度编辑器Ueditor的黑白名单过滤

    黑白名单配置。UEditor针对进入编辑器的富文本内容提供了节点级别的过滤,可以通过该配置的修改来达到控制富文本内容的目的

  • 2017-04-26 23:30:58

    PHP中session变量的销毁

    本篇文章主要是对PHP中session变量的销毁进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助

  • 2017-05-02 17:51:44

    php生成不重复随机字符串

    使用时间戳作为原始字符串,再随机生成五个字符随机插入任意位置,生成新的字符串,保证不重复

  • 2017-05-02 17:54:57

    高并发 php uniqid 不重复唯一标识符生成方案

    PHP uniqid()函数可用于生成不重复的唯一标识符,该函数基于微秒级当前时间戳。在高并发或者间隔时长极短(如循环代码)的情况下,会出现大量重复数据。即使使用了第二个参数,也会重复,最好的方案是结合md5函数来生成唯一ID。