繁体   English   中英

单击更改自定义形状的背景颜色的任何方法

[英]Any way to change background color of custom shape on click

我的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" />

这是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"/>

您可以使用选择器,但要链接整个可绘制对象,而不仅是color 这里

<?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>

你可以像这样理解这个清单

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

创建一个StateList可绘制对象,并为按下状态创建一个不同的可绘制对象。

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM