【发布时间】: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 级别 21
-
stackoverflow.com/a/30931750/3286614 使用它来设置低于 21 层的海拔。
标签: android android-layout android-studio