我有一个TextView,我将其设置为可点击和可聚焦 - 当用户使用轨迹球等聚焦时,如何使其高亮显示为橙色(类似于按钮)?
TextView tv = ...;
tv.setClickable(true);
tv.setFocusable(true);
谢谢
我有一个TextView,我将其设置为可点击和可聚焦 - 当用户使用轨迹球等聚焦时,如何使其高亮显示为橙色(类似于按钮)?
TextView tv = ...;
tv.setClickable(true);
tv.setFocusable(true);
谢谢
这很容易。以下是解决方案。
您有一个TextView元素,其背景属性设置为像这样的@drawable/tbselector
。
<TextView android:text="My text"
android:id="@+id/tv01"
android:layout_width="300dip"
android:layout_height="150dip"
android:layout_gravity="center_horizontal"
android:background="@drawable/tbselector"/>
最后一个属性android:background
是必要的,其他的东西由您决定。
现在您可以在drawable
子目录中创建一个名为tbselector.xml
的文件,其内容如下所示。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/bgdefault"
android:state_focused="false"
android:state_selected="false"/>
<item
android:drawable="@drawable/bgselected"
android:state_focused="true"
android:state_selected="false"/>
</selector>
现在,在你的drawable
子目录中创建一个名为bgdefault.xml
的文件,其内容如下。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="200dip"
android:height="150dip"
android:color="#00FF00"/>
<solid
android:color="#00FF00"/>
</shape>
最后,在您的drawable
子目录中创建一个名为bgselected.xml
的文件,它看起来与其他颜色值相同,例如:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="200dip"
android:height="150dip"
android:color="#FFFF00"/>
<solid
android:color="#FFFF00"/>
</shape>
这就是了,现在您拥有了一个状态相关的TextView背景。但是,您可以决定在选择器XML中设置自己的可绘制图像,完全取决于您。我的值只是随意设置的值,以便向您展示差异。
希望能对您有所帮助。
目前我没有机会尝试它,但是你可以尝试给你的TextView
添加一个OnFocusChangeListener
,然后使用setBackgroundColor(int color)
方法将背景色更改为橙色。