我想在单击时更改textview的背景图像。我对选择器有所了解,我确实喜欢这个:
selector.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" />
但它不起作用。我试图改变bg颜色,在这种情况下它可以工作。我究竟做错了什么?
答案 0 :(得分: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。
答案 1 :(得分: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>
请使用不同的图片。
答案 2 :(得分: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"/>
答案 3 :(得分:0)
你能做什么:
android:onClick="change"
在java中: -
public void onCLick(View view){
TextView text=(TextView) findViewById(R.id.textUp);
text.setBackgroungResource(R.drawable.image);
}
答案 4 :(得分: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" />