【发布时间】:2019-04-03 22:32:11
【问题描述】:
我正在尝试在 Button 上使用 setEnabled(false) 来显示灰色禁用状态...
-
如果我在按钮上强制使用主题,我只能得到想要的结果
android:theme="@style/Button"然后将该 Button 样式上的父级更改为
Theme.AppCompat.Light.DarkActionBar以外的其他内容,例如来自 Material:<style name="Button" parent="android:Theme.Material.Light.NoActionBar">(即使那样,只有按钮背景会改变,文本颜色不会。)
如果我让按钮采用默认设置,当设置为禁用时,我无法看到按钮为灰色。 (我的理解是在使用
Theme.AppCompat.时,Button变成AppCompat.Widget.Button并继承了ColorStateList。)
在我的res\values\styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- only way I could get this to work -->
<style name="Button" parent="android:Theme.Material.Light.NoActionBar">
<!--<item name="colorAccent">@color/butt</item>-->
<!--<item name="colorButtonNormal">@color/butt</item>-->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
在AndroidManifest.xml
<application
android:theme="@style/AppTheme"
( and nothing under <activity> )
我的MainActivity 扩展了Activity
- 但我也尝试过扩展
AppCompatActivity并遇到了同样的问题。
那么这里发生了什么?为什么“禁用按钮”的默认设置不起作用?
- (并且如果我确实继承了
Material主题(如上),为什么按钮文本颜色没有改变?基本上,为什么按钮看起来不像它们在主题中所做的那样编辑?)
还有
-
当我通过
res\color指定自定义ColorStateList时,为什么我不能只覆盖某些状态?我试图只有<item android:state_enabled="false" android:color="#616161" />但它没有用。如果我指定更多
<item>s,我只能使用此方法获得一个灰色的禁用按钮,即使我不关心匹配其他状态。仅当我添加以下内容时才有效:<!-- disabled state --> <item android:state_enabled="false" android:color="#616161" /> <!-- enabled state --> <item android:state_enabled="true" android:color="@color/colorPrimaryLighter" /> <!-- default --> <item android:color="#ffffff"/>
我的supportLibraryVersion = '27.1.1'
我已经看过了
How to setup disabled color of button with AppCompat?
Widget.AppCompat.Button colorButtonNormal shows gray
State list drawable and disabled state
Android - default button style
Android: change color of disabled text using Theme/Style?
When should one use Theme.AppCompat vs ThemeOverlay.AppCompat?
如果我不需要,我不想覆盖默认值并指定我不需要的任何内容。提前致谢!
【问题讨论】:
标签: android android-layout android-appcompat