Android相对布局选择器

Anj*_*ula 3 android relativelayout android-layout

如何更改相对布局形状的颜色?像按钮一样工作.我尝试使用选择器来做到这一点.

这是我使用的代码.

       <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/rlGPS"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:background="@drawable/relative_selector"

            >

            <ImageView
                android:id="@+id/iv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:id="@+id/tvs1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:layout_below="@+id/tvl1"
                android:layout_toLeftOf="@+id/iv1"
                android:text="you can here protect ... "
                android:textColor="@color/gray" />

            <TextView
                android:id="@+id/tvl1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@id/iv1"
                android:text="GPS"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/white" />
        </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

relative_selector.xml在这里,我正在使用选择器进行更改.

<?xml version="1.0" encoding="utf-8"?> 

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 


  <item android:state_focused="true" android:drawable="@drawable/relative_focus"/>
  <item android:state_pressed="true" android:state_enabled="false"
   android:drawable="@drawable/relative_clicked" />
  <item android:drawable="@drawable/relative_background"/>

</selector>
Run Code Online (Sandbox Code Playgroud)

relative_background.xml这是形状.

<?xml version="1.0" encoding="utf-8"?>


<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:padding="10dp"
 android:shape="rectangle">

 <stroke android:width="1dp" android:color="#636161" />

 <padding android:left="5dp"
    android:top="2dp"
    android:right="2dp"
    android:bottom="5dp" />

 <corners android:radius="6dp" />

 <solid android:color="#88ffffff"
     />

</shape>
Run Code Online (Sandbox Code Playgroud)

在"relative_clicked.xml"和"relative_focus.xml"中,我只更改了颜色.

relative_focus.xml

      <stroke android:width="1dp" android:color="#ffff00" /> 
Run Code Online (Sandbox Code Playgroud)

relative_clicked.xml

      <solid android:color="#88ffff00"/> 
Run Code Online (Sandbox Code Playgroud)

所有.xml文件都在drawable文件夹中.我想像按钮一样工作.有人有任何想法吗?

Sam*_*Sam 7

从你的选择器xml:

<item android:state_pressed="true" 
      android:state_enabled="false" 
      android:drawable="@drawable/relative_clicked" />
Run Code Online (Sandbox Code Playgroud)

如果你删除它会工作state_enabled吗?

<item android:state_pressed="true" 
      android:drawable="@drawable/relative_clicked" />
Run Code Online (Sandbox Code Playgroud)