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

3

I am trying to define a gradient background for a toolbar, but I am getting a weird effect where the gradient becomes the background of all the elements inside the toolbar:

toolbar propagating background

Other answers suggest to set the background at layout but I am interested in getting it working at style level. Ay suggestion? Thanks in advance!

My defined styles are the following:

   <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>

And this is the toolbar declaration at my layout 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"
     />

1个回答

2
解决方案非常简单/愚蠢,只需在styles.xml中使用toolbarStyle属性即可:
    <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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接