我的应用中有一个布局,我定义为:
<LinearLayout
android:id="@+id/amountLayout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="@drawable/gradient_effect_green"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp" >
和gradient_effect_green
是:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<
<solid android:color="@color/green_light" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" /></shape>
一切都像魅力一样。现在我想在这个布局上设置监听器,我想为这个布局定义激活状态,但我不知道如何。通过激活状态,我的意思是例如当用户触摸此布局时更改背景颜色。像这样:
android:state_pressed="true" android:drawable="@color/deep_light"
答案 0 :(得分:1)
只需按照步骤
1)gradient_effect_green.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<
<solid android:color="@color/green_light" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" /></shape>
2)gradient_effect_green_selected.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<
<solid android:color="@color/deep_light" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" /></shape>
3)gradient_effect_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/gradient_effect_green_selected" android:state_pressed="true"></item>
<item android:drawable="@drawable/gradient_effect_green_selected" android:state_focused="true"></item>
<item android:drawable="@drawable/gradient_effect_green" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="@drawable/gradient_effect_green_selected" android:state_enabled="false"></item>
</selector>
4)your_layout.xml
<LinearLayout
android:id="@+id/amountLayout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="@drawable/gradient_effect_selector" // CHANGE HERE
android:orientation="vertical"
android:padding="10dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="3dp" >