【发布时间】:2021-04-14 23:34:19
【问题描述】:
参考:How to specify background color in Color State List Resources?
我已按照上面链接中的流行答案。
我有以下可绘制对象;
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:drawable="@color/KeyActionPrimary"/> <!-- enabled -->
<item android:state_enabled="false"
android:drawable="@color/KeyActionButtonDeactivated"/> <!-- disabled -->
<item android:state_pressed="true"
android:drawable="@color/KeyActionPrimary"/> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@color/KeyActionPrimary"/> <!-- focused -->
<item android:drawable="@color/KeyActionPrimary"/> <!-- default -->
</selector>
我了解它将始终遵循优先顺序,因此在当前顺序中仅激活和停用可能会被触发。但是目前我根本没有得到任何结果
我的按钮;
<Button
android:id="@+id/buttonSTART"
android:layout_width="@dimen/btn_width"
android:layout_height="@dimen/btn_height"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="16dp"
android:background="@drawable/state_controlled_key_action_button"
android:padding="@dimen/btn_padding"
android:text="@string/btn_start"
android:textColor="@color/key_action_button_text_color"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnSCORES"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
按钮显示为白色,在我的材质主题中是 ColorPrimary 属性
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Home" parent="Theme.MaterialComponents">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/Primary</item>
<item name="colorPrimaryVariant">@color/PrimaryVariant</item>
<item name="colorSecondary">@color/Secondary</item>
<item name="colorSecondaryVariant">@color/SecondaryVariant</item>
<!--colorBackground appears behind scrollable content and is used for the default window-->
<!--background. colorSurface is mapped to the surface of components such as cards, sheets-->
<!--and menus. colorError is used to indicate an error state for components such as-->
<!--text fields.-->
<item name="android:colorBackground">@color/Background</item>
<item name="colorSurface">@color/Surface</item>
<item name="colorError">@color/Error</item>
<!--"On" colors define how text, icons and strokes are colored in relation to the surface-->
<!--on which they appear.-->
<item name="colorOnPrimary">@color/ColorOnPrimary</item>
<item name="colorOnSecondary">@color/ColorOnSecondary</item>
<item name="colorOnBackground">@color/ColorOnBackground</item>
<item name="colorOnSurface">@color/ColorOnSurface</item>
<item name="colorOnError">@color/ColorOnError</item>
<item name="android:fontFamily">@font/atma_medium</item>
<item name="android:textColor">@color/PrimaryTextColor</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">@color/StatusBar</item>
<!-- Customize your theme here. -->
<item name="actionBarSize">36dip</item>
</style>
</resources>
首先我假设一个主题涵盖了整个应用程序,但如果您明确指定特定按钮的背景,它应该覆盖主题值,如果我错了,请纠正我。
我从 res/colors 下的 colorstatelist 开始了这段旅程。阅读顶部的链接,确认您需要使用drawable。因为我已经有一个颜色状态列表,最初遵循了答案的建议,得到了 0 票......
形状
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@color/primary_button_color" />
<stroke android:width="@dimen/btn_stroke" android:color="@color/primary_button_stroke_color"/>
<corners android:radius="@dimen/btn_radius"/>
</shape>
颜色状态列表
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:color="@color/Primary"/> <!-- pressed -->
<item android:state_enabled="false"
android:color="@color/PrimaryButtonDeactivated"/> <!-- pressed -->
<item android:state_pressed="true"
android:color="@color/Primary"/> <!-- pressed -->
<item android:state_focused="true"
android:color="@color/Primary"/> <!-- focused -->
<item android:color="@color/Primary"/> <!-- default -->
</selector>
这些方法都没有修改我的按钮颜色。我仍然总是看到一个白色的按钮。我故意选择了可怕的颜色以确保变化明显
我做错了什么?你还需要看什么?
更新…………
在与@rcs 讨论之后,我使用了来自 res/color 的原始 colorstatelist 和按钮上的 backgroundtint 属性,它按预期工作
我的最终代码:
对于每个需要不同样式的按钮,我在 res/colors 中有一个按钮颜色或文本颜色文件,如下所示
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="@color/KeyActionColorOnPrimary"/>
<item android:state_enabled="true"
android:color="@color/KeyActionPrimary"/>
<item android:state_enabled="false"
android:color="@color/lime"/>
<item android:state_focused="true"
android:color="@color/KeyActionPrimary"/>
<item android:color="@color/KeyActionPrimary"/>
</selector>
然后在我的按钮 xml 中
android:backgroundTint="@color/key_action_button_color" // for button color
android:textColor="@color/key_action_button_text_color" // for text color
例如
<Button
android:id="@+id/buttonSTART"
android:layout_width="@dimen/btn_width"
android:layout_height="@dimen/btn_height"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="16dp"
android:backgroundTint="@color/key_action_button_color"
android:padding="@dimen/btn_padding"
android:text="@string/btn_start"
android:textColor="@color/key_action_button_text_color"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnSCORES"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
这无需其他样式即可使用。我可以保留我的材料设计主题,而无需将按钮样式设置为 apt compat 样式。我只在布局 xml 中为我想要这些样式的按钮使用上述属性
【问题讨论】: