【问题标题】:How to change color of toolbar options background如何更改工具栏选项背景的颜色
【发布时间】:2016-04-21 20:36:15
【问题描述】:

我想更改工具栏中选项的浅色背景以及尝试复制内容时出现的部分。现在真的很糟糕:

这是我的styles.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    !-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
     <item name="android:textColorPrimary">@color/primary_text</item>
     <item name="android:textColorSecondary">@color/secondary_text</item>

  </style>

  <!-- Application theme. -->
  <style name="MyTheme" parent="MyTheme.Base">
  </style>

  <style name="MyTheme.ToolBar" parent="MyTheme.Base">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
  </style>

  <style name="MyTheme.NavMenu" parent="MyTheme.Base">
    <item name="android:textColorPrimary">@color/gray</item>
  </style>

  <style name="MyTheme.RightMenu" parent="MyTheme.Base">
    <item name="android:textColorPrimary">@color/black</item>
  </style>
</resources>

您可能已经注意到,我使用的是NoActionBar,因为我使用的是工具栏。它在我的 main.xml 上:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
<!-- your content layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <!-- Toolbar instead of ActionBar so the drawer can slide on top -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            app:theme="@style/MyTheme.ToolBar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize" />
    <!-- Real content goes here -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/nav_view_header"
        app:menu="@layout/nav_view_menu"
        app:theme="@style/MyTheme.NavMenu" />
</android.support.v4.widget.DrawerLayout>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    我不确定您要使用 ActionMode 还是 Toolbar: 要更改工具栏的颜色,请查看此链接

    Change Toolbar color in Appcompat 21

    或者改变ActionMode的颜色看这个链接

    How to change ActionMode background color in Android

    【讨论】:

    • 我正在使用支持库。
    • 好的,你能解释一下为什么这个方法在你的情况下不起作用吗?
    【解决方案2】:

    在您的工具栏中试试这个:

    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    

    AppCompat ToolBar popupTheme not used in the ShareAction MenuItem

    这里是AppBarLayout:

    <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbarmain"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/ColorPrimary"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    

    也看看这个:

    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    

    【讨论】:

    • 我应该在哪里应用弹出主题?
    • 弹出主题确实有助于提升用户体验,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    相关资源
    最近更新 更多