【问题标题】:How can I change the summary text color of a Preference programmatically?如何以编程方式更改首选项的摘要文本颜色?
【发布时间】:2014-01-13 22:14:27
【问题描述】:
当用户输入不正确的值时,我试图更改首选项的文本颜色,类似于 Web 表单。我在this question 中找到了如何访问摘要的文本视图,但颜色没有改变。我使用的代码是这样的:
LinearLayout ly = (LinearLayout) userPasswordPref.getView(null, getListView());
TextView summarytv = (TextView) ((RelativeLayout) ly.getChildAt(1)).getChildAt(1);
summarytv.setTextColor(Color.RED);
UserPasswordPref 的类型为 Preference。我怎样才能改变颜色?
【问题讨论】:
标签:
android
sharedpreferences
preferences
preferenceactivity
textcolor
【解决方案1】:
我找到了解决方案,我不得不使用以下代码创建布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/red" />
</LinearLayout>
然后,在我的代码中:
userPasswordPref.setLayoutResource(R.layout.summary_error);
userPasswordPref.setSummary(R.string.error_summary);
其中userPasswordPref 是Preference,summary_error 是上面的布局。
来源:elbauldelprogramador