棒棒糖:禁用按钮 --> 使用哪种样式?

12

我试图追踪,如何 Lollipop 在布局文件中使用 android:enabled="false" 来禁用按钮的显示。

Holo

对于 Holo,很容易:在 styles_holo.xml 中,我找到了样式 Widget.Holo.Button,它给了我一个引用 @drawable/btn_default_holo_dark。在那里,我找到了选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="@drawable/btn_default_disabled_focused_holo_dark" />
<item
android:drawable="@drawable/btn_default_disabled_holo_dark" />
</selector>

棒棒糖

当我试图将相同的逻辑应用于棒棒糖时,我陷入了困境:

在styles_material.xml中,我找到了样式<style name="Widget.Material.Button">,其中我找到了对<item name="background">@drawable/btn_default_material</item>的引用。但是没有选择器??!!取而代之的是:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="@drawable/btn_default_mtrl_shape" />
</ripple>

请问,Lollipop使用哪种特定的样式来表示禁用按钮?非常感谢!

编辑

我可以部分回答自己的问题:在@drawable/btn_default_mtrl_shape中,我找到了一个对<solid android:color="?attr/colorButtonNormal" />的引用,它又指向了@color/btn_default_material_light,其中包括一个选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:alpha="@dimen/disabled_alpha_material_light"
android:color="@color/button_material_light"/>
<item android:color="@color/button_material_light"/>
</selector>

但是alpha值只解释了其中一半。不知何故,Lollipop 还将高度降为 0?


高度不是背景可绘制的一部分,而是由stateListAnimator控制的。再看一下Material按钮样式。 - alanv
1个回答

18

感谢您的部分回答,这是我解决问题的方式。

首先,在“res”文件夹下添加一个名为“color”的新文件夹(如果没有该文件夹)enter image description here

在“color”文件夹中添加一个新的.xml文件(我将称此文件为ButtonColorSelector.xml),我们将在其中创建一个新的ColorStateList如下:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#F2F2F2"/> <!-- disabled -->
    <item android:state_pressed="true" android:color="#FF0000"/> <!-- pressed -->
    <item android:state_focused="true" android:color="#FF0000"/> <!-- focused -->
    <item android:color="#0000FF"/> <!-- default -->
</selector>

第二步:将你提到的那个ripple effect .xml文件添加到 "drawable" 文件夹下,并引用你的colorSelector而不是btn_default_mtrl_shape。我将把这个文件称为 RaisedButton.xml。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item>
        <shape>
            <solid android:color="@color/buttoncolorselector"/>
            <corners android:radius="5dp" />
        </shape>
    </item>
</ripple>

第三步:现在你可以在你的布局中使用你的drawable作为按钮的背景,例如:

<Button
    android:background="@drawable/raisedbutton"
    android:text="@string/SomeButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="DoStuffitto"
    android:enabled="false"
    android:id="@+id/someButton" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接