2013-04-09 184 views
1

我需要更改行視圖背景色爲藍色ListView的行內視圖的顏色,它是這樣的: enter image description here改變背景顏色,當觸及

的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/layout" 
android:layout_width="@dimen/main_info_cell_view_width" 
android:layout_height="@dimen/main_info_cell_view_height" 
android:background="@color/main_info_cell_background_color" > 

<FrameLayout 
    android:id="@+id/pic_frame" 
    android:layout_width="@dimen/channel_pic_background_widht" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" > 

    <View 
     android:id="@+id/channel_pic_background" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/main_info_cell_pic_background_color" /> 

    <ImageView 
     android:id="@+id/channel_pic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:src="@drawable/c__no_image" /> 
</FrameLayout> 

<View 
    android:id="@+id/divider" 
    android:layout_width="@dimen/main_info_cell_divider_widht" 
    android:layout_height="match_parent" 
    android:layout_toRightOf="@+id/pic_frame" 
    android:background="@color/main_info_cell_divider_color" /> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="@dimen/title_margin_left" 
    android:layout_marginRight="@dimen/title_margin_right" 
    android:layout_marginTop="@dimen/title_margin_top" 
    android:layout_toRightOf="@+id/divider" 
    android:ellipsize="end" 
    android:lines="2" 
    android:textColor="@color/main_info_cell_title_color" 
    android:textSize="@dimen/title_text_size" /> 

<TextView 
    android:id="@+id/time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="@dimen/time_margin_bottom" 
    android:layout_marginLeft="@dimen/time_margin_left" 
    android:layout_toRightOf="@+id/divider" 
    android:textColor="@color/main_info_cell_time_color" 
    android:textSize="@dimen/time_text_size" /> 

<TextView 
    android:id="@+id/pos_number" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="@dimen/pos_nr_margin_right" 
    android:textColor="@color/main_info_cell_pos_nr_color" 
    android:textSize="@dimen/pos_nr_size" /> 

</RelativeLayout> 

我嘗試使用選擇器的行,但問題是選擇器只改變視圖的背景顏色和ImageView下的黑色方塊仍然是黑色的,我怎麼能改變它的顏色與行背景?

回答

3

使用StateListDrawable作爲層次結構根/父ViewGroup的背景。

因此,android:background="@drawable/mystatelistdrawable"其中mystatelistdrawable是使用this guide佈置的文件。你可能也想看看android:duplicateParentState

+0

android:duplicateParentState不工作,並且一個stateListDrawable已經爲viewgroup設置了問題是我如何在觸摸時更改子視圖背景顏色以及父背景 – user924941 2013-04-10 12:00:52

+0

我必須錯過上面設置StateListDrawable的位置佈局。它在哪裏? – James 2013-04-10 12:37:54

+0

哦,對不起,我把它添加到代碼中)反正它可以在xml中設置它的效果 – user924941 2013-04-10 12:40:32