如何在 Android 中更改首选项类别的文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11607302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change text color of preference category in Android?
提问by Michael Eilers Smith
The textColor attribute isn't working. Here's my XML:
textColor 属性不起作用。这是我的 XML:
<PreferenceCategory
android:title="Title"
android:textColor="#00FF00">
Any ideas?
有任何想法吗?
回答by AliSh
use this customize PreferenceCategory class :
使用此自定义 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);
}
}
and add this at your Pref.xml file :
并将其添加到您的 Pref.xml 文件中:
<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />
回答by flawyte
One solution is to make a theme for your PreferenceScreen. So in your themes.xml or styles.xml (better to put it in themes.xml) :
一种解决方案是为您的 PreferenceScreen 制作一个主题。所以在你的themes.xml 或styles.xml(最好把它放在themes.xml 中):
<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
<item name="android:textColor">@color/yourCategoryTitleColor</item>
</style>
then in your AndroidManifest.xml :
然后在你的 AndroidManifest.xml 中:
<activity
android:name="MyPreferenceActivity"
...
android:theme="@style/PreferenceScreen" >
</activity>
It worked perfectly for me.
它非常适合我。
回答by Simon
An easy way to do this is to set the custom layout for the preferenceCategory here:
一种简单的方法是在此处为首选项类别设置自定义布局:
<PreferenceCategory
android:layout="@layout/preferences_category"
android:title="Privacy" >
Then set your code inside your preferences_category layout file:
然后在您的首选项_类别布局文件中设置您的代码:
<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"/>
回答by Efren
To change text color of preference category only set a theme to your PreferenceActivity
in your Android Manifest and make sure that colorAccentitem exists. This color is taken by your PreferenceCategory
.
要更改首选项类别的文本颜色,只需PreferenceActivity
在您的 Android 清单中为您设置一个主题,并确保存在colorAccent项目。此颜色由您的PreferenceCategory
.
回答by Bharatesh
Other way around will be mention the theme in your AppTheme, application level
其他方式将在您的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: 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"
/>
For more customization visit v7 Preferences res
如需更多定制,请访问v7 Preferences res
Important: I am using PreferenceFragmentCompatfrom lib v7 Preference.
重要提示:我使用PreferenceFragmentCompat从LIB V7偏好。
回答by Putra Purba
Actually just found out that preference category text using colorAccent
.
If your app didn't using style of colorAccent
, you can go to styles.xml
and find <item name="colorAccent">@color/colorPrimary</item>
, and change the color as you want.
实际上刚刚发现使用colorAccent
.
如果您的应用程序没有使用 样式colorAccent
,您可以去styles.xml
查找<item name="colorAccent">@color/colorPrimary</item>
,并根据需要更改颜色。
回答by Leebeedev
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;
}
}
And in your prefs.xml:
在您的 prefs.xml 中:
<com.your.packagename.MyPreferenceCategory android:title="General">
.
.
.
</com.your.packagename.MyPreferenceCategory>
Or you can also use this answer
或者你也可以使用这个答案
回答by user10320258
Define your own PreferenceTheme and override colors.
定义您自己的 PreferenceTheme 并覆盖颜色。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/AppTheme.PreferenceTheme</item>
</style>
<style name="AppTheme.PreferenceTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="colorAccent">`#color_value`</item>
</style>
回答by Paul Spiesberger
Inspired by @AliSh answer, but I needed to only change the colour of onePreference
text item. So, for all the Kotlin guys out there:
受到@AliSh 答案的启发,但我只需要更改一个Preference
文本项的颜色。所以,对于所有的 Kotlin 人来说:
class TextColorPreference : Preference {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(
context: Context, attrs: AttributeSet,
defStyle: Int
) : super(context, attrs, defStyle)
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
context?.let {
(holder?.findViewById(android.R.id.title) as? TextView)?.setTextColor(
ContextCompat.getColor(
it,
R.color.colorPrimary
)
)
}
}
}
And then put it in your xml/prefs.xml
:
然后把它放在你的xml/prefs.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<this.should.be.your.package.TextColorPreference
android:id="@+id/settings_logout"
android:key="@string/prefs_key_logout"
android:title="@string/settings_logout" />
</PreferenceScreen>
回答by avianey
Using Material Theme, you just have to override the following property :
使用 Material Theme,您只需覆盖以下属性:
<style name="YourTheme" parent="@style/Theme.MaterialComponents">
<item name="colorAccent">@color/your_custom_color</item>
</style>