【问题标题】:Define toolbar gradient in styles.xml在styles.xml 中定义工具栏渐变
【发布时间】:2015-04-22 07:26:49
【问题描述】:

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

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

我定义的样式如下:

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

【问题讨论】:

    标签: android styles android-5.0-lollipop toolbar


    【解决方案1】:

    解决方案就像在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>
    

    【讨论】:

      猜你喜欢
      • 2012-02-22
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多