2013-04-09 185 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