【问题标题】:Style appcompat-v7 Toolbar menu background样式 appcompat-v7 工具栏菜单背景
【发布时间】:2015-04-19 21:59:48
【问题描述】:

我正在尝试设置 appcompat-v7 工具栏的样式,以便为溢出菜单设置不同的背景颜色。 我尝试使用我的应用程序的主题和我的工具栏的样式,但我无法实现它。

这是我的工具栏:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"                                                                                                                              
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:layout_width="match_parent"
    app:theme="@style/AppToolbarTheme"
    android:layout_height="wrap_content">

这是我创建的样式:

    <style name="AppToolbarTheme" parent="Theme.AppCompat.NoActionBar">
       <item name="android:textColorPrimary">@color/white</item>
       <item name="android:textColorSecondary">@color/cian</item>
    </style>

我的主题是扩展 Theme.AppCompat.Light。

有人知道我该怎么做吗?如果无法使用样式,还有其他方法可以实现吗?

【问题讨论】:

标签: android android-layout android-toolbar


【解决方案1】:

将此添加到您的工具栏元素

app:popupTheme="@style/ThemeOverlay.YourPopup"

然后在您的styles.xml 中定义弹出菜单样式

<style name="ThemeOverlay.YourPopup" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:colorBackground">@color/mtrl_white_100</item>
    <item name="android:textColor">@color/mtrl_light_blue_900</item>
</style>

<style name="ThemeOverlay.YourPopup" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:colorBackground">@color/mtrl_white_100</item>
    <item name="android:textColorPrimary">@color/mtrl_light_blue_900</item>
</style>

请注意,您需要使用android:colorBackground,而不是android:background。后者将应用于没有背景的所有内容(这里是菜单本身和每个菜单项),前者仅适用于弹出菜单。

更新:textColorPrimarytextColor 也是如此。

  • 弹出菜单项定义android:textColor="?android:textColorPrimary"
  • android:textColorPrimary 是一个 theme 属性,它是在主题上定义的。
  • android:textColor 是一个 style 属性,它在小部件上定义。
  • 如果我们在主题中定义 android:textColor,它将应用于未定义自己的 android:textColor 的每个小部件。

【讨论】:

  • 这对我不起作用,但this 起作用了。
  • @Android 我弄错了,感谢您的注意,现在应该可以了。
【解决方案2】:

将此添加到您的activity.xml 文件中的工具栏:-

app:popupTheme="@style/ThemeOverlay.YourApp"

然后在你的styles.xml中添加这个:-

<style name="ThemeOverlay.YourApp" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:colorBackground">@android:color/darker_gray</item>
        <item name="android:textColorPrimary">@color/TextColorPrimary</item>
    </style>

【讨论】:

    【解决方案3】:

    在使用AppCompat 属性时,不要使用android 命名空间。修改你的代码如下:

    <style name="AppToolbarTheme" parent="Theme.AppCompat.NoActionBar">
           <item name="textColorPrimary">@color/white</item>
           <item name="textColorSecondary">@color/cian</item>
    </style>
    

    【讨论】:

    • 这适用于工具栏上我的图标的颜色。谢谢!
    • 找不到与给定名称匹配的资源:attr 'textColorPrimary'。
    • @dr4cul4 找到这样的&lt;item name="android:textColorPrimary"&gt;@color/white&lt;/item&gt;
    • android:textColorPrimaryandroid:textColorPrimary 是一些没有 appcompat 对应的属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 2015-05-16
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多