我动态生成 TextView,其工作方式类似于按钮。现在我想在他们受到压力时突出显示他们。比如更改文本颜色或背景颜色。
我尝试过使用选择器,但它不起作用。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ffffff"/>
<item android:state_pressed="true" android:state_enabled="false" android:color="#ffffff" />
</selector>
这是我创建 TextView 的循环。
int z = 0;
for (MOKGenericDataItem d : data) {
if (d.getButtonText() != null) {
final int pagePosition = z;
TextView btn = new TextView(getActivity());
btn.setId(z);
final int id_ = btn.getId();
btn.setText(d.getButtonText());
btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
btn.setGravity(Gravity.CENTER);
mLineareLayoutViewPagerButtons.addView(btn);
btn1 = ((TextView) view.findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewPager.setCurrentItem(pagePosition,false);
}
});
}
z++;
}
首先,您的这一行会产生歧义,因为您将变量名称设为 btn1 (将其与按钮相关)并且您正在引用TextView
,
btn1 = ((TextView) view.findViewById(id_));
Anyways,一步一步走下去,
-
创建一个像这样的xmllabel_bg.xml
就像下面的drawable
folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressed_color"
android:state_pressed="true" />
<item android:drawable="@drawable/normal_color" />
</selector>
-
In values
文件夹创建另一个xml
像下面这样,命名为labelcolors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="pressed_color">#7ec0ee</drawable> <!--custom color for pressed state -->
<drawable name="normal_color">#00FFFFFF</drawable> <!--transperent color for normal state -->
</resources>
-
现在将标签的背景设置为label_bg.xml
<TextView
android:id="@+id/yourlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="760dp"
android:layout_marginTop="515dp"
android:background="@drawable/label_bg" <!--like this-->
android:text="LabelText"
android:textSize="20dp" />
当您动态添加视图时,您需要以编程方式设置每个 textView 的背景.对于那个电话setBackgroundResource()
on the textview
创建并设置对象label.xml
作为背景
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)