android - 在 styles.xml 中定义工具栏渐变

标签 android styles android-5.0-lollipop toolbar

我试图为工具栏定义一个渐变背景,但我得到了一个奇怪的效果,渐变变成了工具栏内所有元素的背景:

toolbar propagating background

其他答案建议在布局中设置背景,但我有兴趣让它在样式级别工作。有什么建议吗?提前致谢!

我定义的样式如下:

   <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
      <!-- This puts the status bar translucent -->
      <item name="android:windowDrawsSystemBarBackgrounds">true</item>
      <item name="android:windowTranslucentStatus">true</item>

      <!-- set actionBar style. This att is referenced from layout to set the style-->
      <item name="android:actionBarStyle">@style/AppThemeToolBar</item>
   </style>

   <style name="AppThemeToolBar" parent="@style/Theme.AppCompat">
        <item name="android:icon">@drawable/ic_logo</item>
        <item name="android:background">@drawable/toolbar_background</item>
        <!-- trying to set a text without background but is being ignored -->
        <item name="android:titleTextStyle">@style/ToolBarTitle</item>
        <!-- also ignored -->
        <item name="android:titleTextAppearance">@style/ToolBarTitle</style>
   </style>

这是我布局 xml 中的工具栏声明:

<Toolbar
     android:id="@+id/toolbar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:contentInsetEnd="0dp"
     app:contentInsetStart="0dp"
     android:layout_gravity="top"
     <!-- here actionBarStyle att is read -->
     app:theme="?attr/actionBarStyle"
     />

最佳答案

解决方案就像在 styles.xml 中使用 toolbarStyle att 一样简单/愚蠢:

    <style name="MyTheme">
       <item name="toolbarStyle">@style/NeoToolbarStyle</item>
       <item name="toolbarNavigationButtonStyle">@style/ToolbarNavigationButtonStyle</item>
   </style>

当然,我的工具栏样式可以像往常一样与 android:background 一起使用,而不会出现奇怪的效果:

<style name="AppThemeToolBar" parent="@style/Widget.AppCompat.Toolbar">
    <item name="android:background">@drawable/my_beautiful_gradient</item>
</style>

关于android - 在 styles.xml 中定义工具栏渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29790589/

相关文章:

android - 无法直接在 ADT-1 开发工具包上调试

Android OpenGL 演示 "No config chosen"

javascript - 在没有jquery的情况下检测html输入中的Enter按键

html - 当在 ul li a 中一起使用 float 和 list-style 属性时,列表圆圈似乎在列表项上方

java - 发送短信 - 正文未在 Lollipop 中传输

android - 如何将其他 Intent 值传递给 ACTION_OPEN_DOCUMENT_TREE Intent

java - 如何使用 post 将 json 对象发送到网络服务器

java - 我无法循环遍历 JSON 数组来获取下一个数组

html - 在 col 12 div 下的 Bootstrap 表格网格上添加滚动条

WPF:在样式和/或数据模板 (MVVM) 中绑定(bind)命令?