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