棒棒糖:禁用按钮 - >哪种风格?

Mic*_*rix 12 android material-design android-5.0-lollipop

我试图追踪Lollipop如何显示一个按钮,该按钮android:enabled="false"在布局文件中被禁用.

霍洛

使用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>
Run Code Online (Sandbox Code Playgroud)

棒糖

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

在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>
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下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>
Run Code Online (Sandbox Code Playgroud)

但是这个alpha值只能解释其中的一半.不知怎的,Lollipop还将海拔降低到0?

小智 18

这是我解决这个问题的方法,这要归功于你的部分答案.

首先:在"res"文件夹下添加新文件夹"color",如果它不存在的话.在此输入图像描述

在"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>
Run Code Online (Sandbox Code Playgroud)

第二步:在"drawable"文件夹下添加你提到的涟漪效果.xml文件并引用你的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>
Run Code Online (Sandbox Code Playgroud)

第三:现在您可以在布局中使用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" />
Run Code Online (Sandbox Code Playgroud)