2014-10-11 40 views
0

我有我的應用程序佈局,我定義爲:設置的激活狀態形狀的Android

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

回答

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" > 
+0

你是我的英雄! – A23149577 2014-10-11 06:26:17

+0

感謝您的讚揚 – 2014-10-11 06:27:08