如何在 Androidx 中更改偏好的文本颜色

我想在 Preference.xml 文件中更改 Preference 的文本颜色


XML代码


<?xml version="1.0" encoding="utf-8"?>

<androidx.preference.PreferenceScreen

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.preference.Preference

        android:key="pref_entries"

        android:title="@string/settings_entries"

        android:summary="0"

        android:enabled="true"

        app:iconSpaceReserved="false" />

</androidx.preference.PreferenceScreen>

我尝试在 xml 文件中添加 textcolor 但它不起作用

Java代码

 prefEntries = pm.findPreference(Const.PREF_ENTRIES);

如何更改首选文本颜色



忽然笑
浏览 124回答 1
1回答

智慧大石

使用此自定义 PreferenceCategory 类:public class MyPreferenceCategory extends PreferenceCategory {&nbsp; &nbsp; public MyPreferenceCategory(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public MyPreferenceCategory(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public MyPreferenceCategory(Context context, AttributeSet attrs,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int defStyle) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onBindView(View view) {&nbsp; &nbsp; &nbsp; &nbsp; super.onBindView(view);&nbsp; &nbsp; &nbsp; &nbsp; TextView titleView = (TextView) view.findViewById(android.R.id.title);&nbsp; &nbsp; &nbsp; &nbsp; titleView.setTextColor(Color.RED);&nbsp; &nbsp; }}并将其添加到您的 Pref.xml 文件中:<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" />方法 2:一种简单的方法是在此处设置自定义布局preferenceCategory:<PreferenceCategory&nbsp; &nbsp; android:layout="@layout/preferences_category"&nbsp; &nbsp; android:title="Privacy">一种简单的方法是在此处为 preferenceCategory 设置自定义布局:<PreferenceCategory&nbsp; &nbsp; android:layout="@layout/preferences_category"&nbsp; &nbsp; android:title="Privacy" >然后在布局文件中设置代码preferences_category:<TextView&nbsp; &nbsp; android:id="@android:id/title"&nbsp; &nbsp; android:textColor="@color/deep_orange_500"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:textSize="16sp"&nbsp; &nbsp; android:textStyle="bold"&nbsp; &nbsp; android:textAllCaps="true"/>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java