如何在按下背景布局的同时在一个图像视图上应用选择器

时间:2013-02-18 14:30:47

标签: android android-layout android-selector

我有一个包含图标图像和textview的布局。 我想要的是在按下父布局的同时更改图标图像的背景。 我已经尝试了选择器功能,但它只适用于正在按下的视图。如果我按下图标图像应用选择比它运行良好但我想要的是在用户按下其整个父布局时更改图标图像。 这是我的布局xml文件

<RelativeLayout
                android:id="@+id/home_edit_profile"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" 
                 >



                <RelativeLayout
                    android:id="@+id/first_row_image_container"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true" 
                    android:layout_marginLeft="20dp">

                    <ImageView
                        android:id="@+id/image_edit_profile"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:duplicateParentState="true"
                        android:src="@drawable/home_page_edit_profile_press" 
                        />
                </RelativeLayout>

                <RelativeLayout

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@+id/first_row_image_container" 
                    android:layout_toLeftOf="@+id/first_row_arrow_container"
                    android:paddingLeft="15dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="false"
                        android:text="Edit Profile"
                        android:textColor="#ffffff"
                        android:duplicateParentState="true"
                        android:textAppearance="?android:attr/textAppearanceLarge" />

                </RelativeLayout>

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/home_page_saparatar_line" 
                    android:layout_alignParentBottom="true"/>

            </RelativeLayout>

1 个答案:

答案 0 :(得分:3)

//您需要添加此行android:duplicateParentState="true"

在你的图标和textview中。

android:duplicateParentState - 
When this attribute is set to true, the view gets its drawable state 
(focused, pressed, etc.) from its direct parent rather than from itself.