【问题标题】:Android grey out a button programmatically which is set as backgroundTint in the xml?Android以编程方式将在xml中设置为backgroundTint的按钮变灰?
【发布时间】:2017-07-12 11:37:06
【问题描述】:

我已经像这样在 xml 中定义了我的按钮。

  <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:text="@string/button_node"
    android:textColor="@android:color/white"
    android:backgroundTint="@color/colorAccent" />

我需要在代码中将此按钮设为灰色。我已经使用下面的代码行来更改它。

button.setBackgroundResource(R.drawable.button_grey);
button.setEnabled(false);
button.setClickable(false)

我的 button_grey.xml 文件是

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:angle="90"
                android:endColor="@color/colorGray"
                android:startColor="@color/colorGray" />
            <corners android:radius="4dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="1.5dp"
        android:right="0.5dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/cardview_shadow_start_color" />
            <corners android:radius="4dp" />
        </shape>
    </item>
</layer-list>

更改按钮后看起来像这样

但实际上我需要通过将颜色更改为灰色来使按钮看起来像下面。 请注意:- 它应该从 API 级别 18 开始工作。

【问题讨论】:

  • 您不是刚刚在代码中将按钮背景设置为灰色吗?如果您希望按钮为蓝色,带有阴影,您可以将背景设置为蓝色并为其赋予高度。
  • 海拔要求最低 API 级别 21
  • stackoverflow.com/a/30931750/3286614 使用它来设置低于 21 层的海拔。

标签: android android-layout android-studio


【解决方案1】:

试试这个就行了

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item android:right="5dp" android:top="5dp">
                <shape>
                    <corners android:radius="3dp" />
                    <solid android:color="#D6D6D6" />
                </shape>
            </item>
            <item android:bottom="2dp" android:left="2dp">
                <shape>
                    <gradient android:angle="270"
                        android:endColor="#24b8eb" android:startColor="#24b8eb" />
                    <stroke android:width="1dp" android:color="#BABABA" />
                    <corners android:radius="4dp" />
                    <padding android:bottom="10dp" android:left="10dp"
                        android:right="10dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>

</selector>

http://mobilenext.net/4-material-design-tweaks-pre-lollipop-android-devices/

http://www.techrepublic.com/article/android-lollipop-material-design-trick-offers-a-more-polished-ux/

【讨论】:

  • 感谢 Aditya 你的回答帮助了我!!
  • @somia 很高兴为您提供帮助,祝您编码愉快 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多