0

单击它时,我想更改文本视图的背景图像。我对选择器有疑问,我确实喜欢这样:

选择器.xml

<?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/img1" /> <!-- pressed -->
   <item android:drawable="@drawable/img2" /> <!-- default -->
</selector>

然后在布局文件中:

<TextView
        android:id="@+id/textUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:background="@drawable/selector"
        android:clickable="true" />

但它不起作用。我试图改变背景颜色,在这种情况下它可以工作。我究竟做错了什么?

4

5 回答 5

1

您应该像这样创建一个新的可绘制 xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--item android:drawable="@drawable/iv_circle_checked" android:state_pressed="true" />
    <item android:drawable="@drawable/iv_circle_unchecked" /--> 


    <item android:drawable="@drawable/iv_circle_checked" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/iv_circle_checked" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@drawable/iv_circle_checked" android:state_enabled="true" android:state_selected="true"/>
    <item android:drawable="@drawable/iv_circle_unchecked" android:state_enabled="true" android:state_selected="false"/>
    <item android:drawable="@drawable/iv_circle_unchecked" android:state_enabled="true"></item>
</selector>

当你创建这个时,你应该在你的背景中添加这个 xml。

于 2013-11-07T11:42:26.310 回答
0

尝试使用scaleType="fitXY"

<TextView
        android:id="@+id/textUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:scaleType="fitXY"
        android:background="@drawable/selector"
        android:clickable="true" />
于 2013-09-20T11:06:23.643 回答
0
// try this way

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

    <item android:drawable="@drawable/img1" android:state_checked="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/img2" android:state_checked="false" android:state_pressed="false"/>
    <item android:drawable="@drawable/img1" android:state_checked="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/img2" android:state_checked="false" android:state_pressed="true"/>

</selector>


<CheckBox
  android:id="@+id/chk"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hello"
  style="@android:style/Widget.TextView"
  android:background="@drawable/selector"
  android:gravity="center"
  android:checked="true"
  android:button="@null"/>
于 2013-09-20T10:04:05.520 回答
0
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/addgrnbtnhov"
          android:state_pressed="true" 

          />
    <item android:drawable="@drawable/addgrnbtn" /> 
</selector>

请使用不同的图像。

于 2013-09-20T09:56:17.517 回答
0

你可以做什么:

android:onClick="change"

在java中: -

public void onCLick(View view){
TextView text=(TextView) findViewById(R.id.textUp);
text.setBackgroungResource(R.drawable.image);
}
于 2013-09-20T10:04:51.930 回答