【问题标题】:How To Change Text Color Of Preference In Androidx如何在 Androidx 中更改首选项的文本颜色
【发布时间】:2019-12-31 20:27:39
【问题描述】:

我想在 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);
       

如何更改首选项文本颜色

【问题讨论】:

    标签: java android sharedpreferences


    【解决方案1】:

    使用这个自定义的 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" />
    

    方法二: 一个简单的方法是在这里为preferenceCategory 设置自定义布局:

    <PreferenceCategory
        android:layout="@layout/preferences_category"
        android:title="Privacy">
    

    一种简单的方法是在此处为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"/>
    

    【讨论】:

    • 但我也在那里使用摘要,它有 2 个文本视图
    • 然后按照方法2。
    • 超级方法 2 也适用于 androidx.preference.*
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    相关资源
    最近更新 更多