我试图为工具栏定义一个渐变背景,但我得到了一个奇怪的效果,渐变变成了工具栏内所有元素的背景:
其他答案建议在布局中设置背景,但我有兴趣让它在样式级别工作。有什么建议吗?提前致谢!
我定义的样式如下:
<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/