我试图在我的应用中为按钮创建样式。为了做到这一点,我创建了一个带有角标签的可绘制文件。我还为不同的按钮状态(正常和禁用)创建了不同的样式。当我在不设置背景属性的情况下应用样式时,按钮具有正确的颜色。但是,当我用我的可绘制文件设置背景属性时,style.xml中定义的文本颜色起作用,但不起作用。下面是代码:
值/ style.xml:当定义形状时,Android不应用背景色
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">@color/background</item>
<item name="android:textColor">@color/text_color</item>
</style>
<style name="ButtonStyle">
<item name="android:background">@color/button_color</item>
</style>
<style name="ButtonDisableStyle">
<item name="android:background">@color/button_color_disabled</item>
<item name="android:textColor">@color/buttonText_color_disabled</item>
</style>
绘制/ button_shape.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="5dp" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
main_activity:
<Button
android:id="@+id/newButton"
android:text="@string/generate_first"
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="30"
android:background="@drawable/button_shape"
/>
我看到差不多的几个后问题,我知道可以在drawable/button_shape.xml中定义按钮的背景颜色,但是我想保留这个样式并且使其工作(我想要的形状和风格分开)。
你有什么想法吗?
出现这种情况的原因是因为你是压倒一切通过为您的视图设置一个明确的风格背景。我不确定在你的情况下使用xml的方法。你可以创建一个自定义的按钮视图并从那里控制事物,但它不是对你的问题的直接回答。 – zgc7009
做我想在这里做的事情就是让我通过改变风格来改变所有的应用程序设计。由于我的设计是暂时的,我不想创建自定义视图,但我可能会在最终版本中完成。但是,谢谢你的回答。也许我问的是不可能的。无论如何,我会继续搜索。 – Mtoypc