textColor屬性不起作用。這是我的XML:如何更改Android中偏好類別的文本顏色?
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
任何想法?
textColor屬性不起作用。這是我的XML:如何更改Android中偏好類別的文本顏色?
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
任何想法?
使用這種定製PreferenceCategory類:
public class MyPreferenceCategory extends PreferenceCategory {
public MyPreferenceCategory(Context context) {
super(context);
}
public MyPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setTextColor(Color.RED);
}
}
,並在您的Pref.xml文件補充一點:
<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />
一種解決方案是創建一個主題爲您PreferenceScreen。 因此,在您的themes.xml或styles.xml(更好地把它的themes.xml):
<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
<item name="android:textColor">@color/yourCategoryTitleColor</item>
</style>
然後在AndroidManifest.xml中:
<activity
android:name="MyPreferenceActivity"
...
android:theme="@style/PreferenceScreen" >
</activity>
它的工作完美的我。
對我來說,這是正確的答案 – Javi 2017-03-01 11:24:38
public class MyPreferenceCategory extends PreferenceCategory {
public MyPreferenceCategory(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected View onCreateView(ViewGroup parent) {
// It's just a TextView!
TextView categoryTitle = (TextView)super.onCreateView(parent);
categoryTitle.setTextColor(parent.getResources().getColor(R.color.orange));
return categoryTitle;
}
}
而在你的prefs.xml:
<com.your.packagename.MyPreferenceCategory android:title="General">
.
.
.
</com.your.packagename.MyPreferenceCategory>
或者你也可以使用這個answer
這對我來說是一個完美的修改。 而不是 'categoryTitle.setTextColor(parent.getResources()的getColor(R.color.orange));' 我不得不使用 'categoryTitle.setTextColor(Color.BLACK);' – ckn 2015-11-14 05:54:23
一個簡單的方法來做到這一點是在這裏設置了preferenceCategory自定義佈局:
<PreferenceCategory
android:layout="@layout/preferences_category"
android:title="Privacy" >
然後在您的preferences_category佈局文件中設置您的代碼:
<TextView
android:id="@android:id/title"
android:textColor="@color/deep_orange_500"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textAllCaps="true"/>
其他方式將提及的主題在你的AppTheme,應用水平
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
.....//your other items
<item name="preferenceTheme">@style/PrefTheme</item>
</style>
<style name="PrefTheme" parent="@style/PreferenceThemeOverlay">
<item name="preferenceCategoryStyle">@style/CategoryStyle</item>
</style>
<style name="CategoryStyle" parent="Preference.Category">
<item name="android:layout">@layout/pref_category_view</item>
</style>
XML:pref_category_view
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@android:id/title"
style="?android:attr/listSeparatorTextViewStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:textColor="@color/red"
android:layout_height="wrap_content"
/>
需要更多定製訪問v7 Preferences res
重要:我使用PreferenceFragmentCompat從lib v7 Preference。
Happy_Coding;
要更改偏好類別的文本顏色,請在您的Android清單中將主題設置爲您的PreferenceActivity
,並確保colorAccent項存在。這種顏色是由您的PreferenceCategory
拍攝的。
這是正確的答案!將
其實剛剛發現使用colorAccent
的偏好分類文本。
如果您的應用沒有使用colorAccent
的樣式,則可以轉到styles.xml
並找到<item name="colorAccent">@color/colorPrimary</item>
,然後根據需要更改顏色。
國際海事組織,這是作爲棒棒糖和材料設計的正確答案。參考 - http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/frameworks/base/core/res/res/layout/preference_category_material.xml? AV = F – Ginandi 2017-12-12 21:21:26
我發現'findViewById()'在'onBindView()'中仍然不起作用。我在'onCreateView()'中做了它,而不是工作。 – ryan 2013-09-20 01:58:08
'titleView.setTextColor(Color.RED);'拋出'NullPointerException'。任何修復? – 2015-11-25 12:49:19
該修復程序本身就是第一個註釋... – Rohan 2016-04-20 09:04:46