回答

2

嘗試......

1.創建背景繪製了一個ViewGroup

表1.A顏色資源:(colors.xml)

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="colorPrimary">#3F51B5</color> 
    <color name="colorPrimaryDark">#303F9F</color> 
    <color name="colorAccent">#FF4081</color> 

    <color name="blue_grey_200">#B0BEC5</color> 
</resources> 

1.B.的ViewGroup正常狀態:(drawable_viewgroup_normal.xml)

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<solid android:color="@android:color/transparent" /> 
<stroke 
    android:width="1dp" 
    android:color="@color/blue_grey_200" /> 
</shape> 

1.C.的ViewGroup按下狀態:(drawable_viewgroup_press.xml)

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<solid android:color="@color/blue_grey_200" /> 
<stroke 
    android:width="0.7dp" 
    android:color="@color/blue_grey_200" /> 
</shape> 

1.D. ViewGroup中選擇背景:(selector_viewgroup.xml)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/drawable_viewgroup_press" android:state_pressed="true"/> 
    <item android:drawable="@drawable/drawable_viewgroup_press" android:state_focused="true"/> 
    <item android:drawable="@drawable/drawable_viewgroup_normal"/> 
</selector> 

2.一個ViewGroup的孩子創建選擇文本顏色(的TextView) selector_viewgroup_child_textcolor.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:state_pressed="false" android:color="@color/colorAccent"/> 
    <item android:state_focused="true" android:state_pressed="true" android:color="@android:color/white"/> 
    <item android:state_focused="false" android:state_pressed="true" android:color="@android:color/white"/> 
    <item android:color="@color/colorAccent"/> 
</selector> 

3.將選擇可繪製到ViewGroup中和他們的孩子

<LinearLayout 
    android:id="@+id/l1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:background="@drawable/selector_viewgroup" 
    android:clickable="true" 
    android:padding="@dimen/margin8"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Hello World!" 
     android:textColor="@drawable/selector_viewgroup_child_textcolor" /> 
</LinearLayout> 

4.結果

正常狀態:

Normal state

按下狀態:

Pressed state

+0

非常感謝你,我已經解決它作爲你的方法。 – liangzhitao

+0

請接受我的回答,如果對你有用。 –

0

,那是因爲你的TextView顏色與選擇文件 「selector_viewgroup_child_textcolor.xml」 有關,你已經設置了按狀態和正常狀態,如果你想textview的顏色是不可變的,請確保你的textcolor不是指任何sele tor文件