【发布时间】:2020-08-09 11:49:08
【问题描述】:
我有这个按钮、样式和颜色状态文件:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1"
style="@style/btnGreen"/>
<style name="btnGreen" parent="Widget.MaterialComponents.Button">
<item name="android:textStyle">bold</item>
<item name="backgroundTint">@color/color_states_btn_green</item>
</style>
颜色状态:color_states_btn_green.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#00FF00" android:state_enabled="true" />
<item android:alpha="0.10" android:color="#00FF00"/>
</selector>
目标是:
启用按钮 => 启用 Widget.MaterialComponents.Button
样式
按钮禁用 => Widget.MaterialComponents.Button.OutlinedButton 的禁用样式
我需要这样的东西(伪代码):
<selector>
<item style="Widget.MaterialComponents.Button.OutlinedButton" android:state_enabled="false"/>
</selector>
我怎样才能做到这一点? 我不会以编程方式更改按钮属性,因为在不重新创建的情况下在昼夜之间切换会出现问题。谢谢。
【问题讨论】:
-
为什么不在按钮上使用
onClick()来改变按钮的形状? -
you can't change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML来自Christopher Orr... -
这种方式不能改变样式,但是你可以改变所有你需要的属性。
-
我没有以编程方式更改按钮属性,因为在不重新创建的情况下在昼夜之间切换会出现问题。
标签: android android-button android-styles material-components-android