Android 焦点或单击时更改按钮外观?
我试图用背景图像制作一个按钮Android 焦点或单击时更改按钮外观?,android,Android,我试图用背景图像制作一个按钮 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5px" android:layout_gravity="center_vertical" android:id="@+id/button1" android:background="@drawable/imagename"&g
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5px"
android:layout_gravity="center_vertical"
android:id="@+id/button1"
android:background="@drawable/imagename">
</Button>
我想让它将是不同的(阴影,亮度等),当用户点击或触摸按钮
您知道,标准按钮在聚焦或单击时会改变颜色。
我正试着这样做
怎么做
提前感谢。您可以使用选择器XML并将其指定为按钮背景。要了解其工作原理,请查看。为此,您必须使用按钮的不同状态创建自定义绘图。首先进入res->drawable,右键单击并添加新的drawable文件。将其命名为按钮\默认值。将以下代码添加到该文件中
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_default_click" android:state_pressed="true" />
<item android:drawable="@drawable/button_default_click" android:state_focused="true" />
<item android:drawable="@drawable/button_default_click" android:state_active="true" />
<item android:drawable="@drawable/button_default_normal" />
</selector>
在这个文件中,我们定义了当跳跳虎处于不同状态时需要使用的可绘制按钮。
现在获得了drawable文件夹,并创建了一个名为button\u default\u的新drawable,单击并添加以下代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp" />
<gradient
android:angle="90"
android:endColor="@color/white"
android:startColor="@color/gray_light"
android:type="linear" />
<stroke
android:width="1dp"
android:color="@color/gray_dark" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp" />
<stroke
android:width="1dp"
android:color="@color/gray_light_300" />
<gradient
android:angle="90"
android:endColor="@color/white"
android:startColor="@color/gray_light"
android:type="linear" />
</shape>
您可以根据自己喜欢的颜色更改这些颜色属性。
现在创建另一个名为button\u default\u normal的可绘制图形。这是可绘制的,使用时没有任何触发器。将下面的代码添加到该代码中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp" />
<gradient
android:angle="90"
android:endColor="@color/white"
android:startColor="@color/gray_light"
android:type="linear" />
<stroke
android:width="1dp"
android:color="@color/gray_dark" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp" />
<stroke
android:width="1dp"
android:color="@color/gray_light_300" />
<gradient
android:angle="90"
android:endColor="@color/white"
android:startColor="@color/gray_light"
android:type="linear" />
</shape>
不,我们必须将创建的选择器drawable分配给按钮
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5px"
android:layout_gravity="center_vertical"
android:id="@+id/button1"
android:background="@drawable/button_default.xml">
</Button>