1

我试图改变偏好的文本颜色,当用户输入一个不正确的值,类似于web窗体。我在this question中发现了如何访问摘要的文本视图,但颜色未被更改。我使用的代码是这样的:如何以编程方式更改偏好设置的摘要文本颜色?

LinearLayout ly = (LinearLayout) userPasswordPref.getView(null, getListView()); 
TextView summarytv = (TextView) ((RelativeLayout) ly.getChildAt(1)).getChildAt(1); 
summarytv.setTextColor(Color.RED); 

UserPasswordPrefPreference类型。我怎么能改变颜色?

回答

2

我找到了一个解决方案,我不得不用下面的代码创建一个布局:

<?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); 

userPasswordPrefPreferencesummary_error是上面的布局。

来源elbauldelprogramador

相关问题