android - Lollipop : Disabled button --> which style?

标签 android android-5.0-lollipop material-design

我试图追踪,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>

Lollipop

当我尝试将相同的逻辑应用于 Lollipop 时,我卡住了:

在 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?

最佳答案

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

首先:在“res”文件夹下添加新文件夹“color”,如果它不存在的话。 enter image description here

在“颜色”文件夹中添加新的 .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>

第二:在“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>

第三:现在您可以在布局中使用可绘制对象作为按钮背景,如下所示:

<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" />

关于android - Lollipop : Disabled button --> which style?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27926507/

相关文章:

未找到用于 native 代码编译的 Android bitmap.h

android - 如何在代码中检查主屏幕是否在 android 中(在所有设备上)?

android - 尝试构建共享元素 Lollipop 过渡时过渡名称为空

android - Android Lollipop 中的字体提升问题

android - Material 芯片 - 找不到资源 ID(颜色/图标)

android - CollapsingToolbarLayout with tablayout & image as collapsing with sticky toolbar

android - Observable 在 Kotlin 中列出

android - Android 上的群组通知

android - 在 CordinatorLayout 中的 ImageView 底部添加阴影

java - 在 http 客户端服务中保存 CookieStore