Android背景选择器和textcolor选择器冲突

时间:2016-03-18 03:22:40

标签: android selector textview android-selector android-viewgroup

我为ViewGroup设置了一个背景选择器,这个ViewGroup有一个子TextView,我将一个选择器设置为textColor,然后按下这个ViewGroup,TextView'textColor在按下状态下被解除。

2 个答案:

答案 0 :(得分: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。将选择器drawable应用于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

答案 1 :(得分:0)

它是因为你的textview颜色与选择器文件&#34; selector_viewgroup_child_textcolor.xml&#34;有关,你设置了按状态和正常状态,如果你想textview的颜色是不可变的,确保您的textcolor不引用任何seletor文件