有任何方法可以在点击时更改自定义形状的背景颜色

时间:2022-11-20 13:58:32

I have a custom shape for my ListView background. But now it will not change color on click. Is there any way of doing this? Here is my xml for the ListView:

我的ListView背景有自定义形状。但现在它不会改变点击的颜色。有没有办法做到这一点?这是我的ListView的xml:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"

    android:textSize="25sp"
    android:textColor="#ff8d8d8d"/>
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textView"
    android:layout_alignParentLeft="true"
    android:textColor="#ff8d8d8d"
    android:textSize="25sp" />
<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textView1"
    android:layout_alignParentRight="true"
    android:textColor="#ff8d8d8d"
    android:textSize="25sp" />

Here is the CustomShape:

这是CustomShape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<gradient android:startColor="#ffffff"
    android:endColor="#ffd6d4d6"
    android:angle="270"
/>
<corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"
    android:topLeftRadius="10dp" android:topRightRadius="10dp"/>

2 个解决方案

#1


You can use a selector, but you will be chaining the whole drawable, not only the color. See here

您可以使用选择器,但您将链接整个drawable,而不仅仅是颜色。看这里

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed_yellow"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused_orange"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_normal_green" />
</selector>

You can understand this list like

你可以理解这个列表

switch(state){
  case pressed:
     use some drawable
     break;
   case focused:
     use some other drawable
     break;
   default:
    use the default drawable
}

#2


Create a StateList drawable with a different drawable for the pressed state.

使用不同的drawable为压缩状态创建一个StateList drawable。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

#1


You can use a selector, but you will be chaining the whole drawable, not only the color. See here

您可以使用选择器,但您将链接整个drawable,而不仅仅是颜色。看这里

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed_yellow"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused_orange"
          android:state_focused="true" />
    <item android:drawable="@drawable/button_normal_green" />
</selector>

You can understand this list like

你可以理解这个列表

switch(state){
  case pressed:
     use some drawable
     break;
   case focused:
     use some other drawable
     break;
   default:
    use the default drawable
}

#2


Create a StateList drawable with a different drawable for the pressed state.

使用不同的drawable为压缩状态创建一个StateList drawable。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>