如何在Androidx中更改首选项的文本颜色

时间:2019-08-27 15:54:25

标签: java android sharedpreferences

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

如何更改首选文本颜色

1 个答案:

答案 0 :(得分:0)

使用此自定义PreferenceCategory类:

filtered_data = 'secondaryPort = 7504'
=> 'secondaryPort = 7504'

# with literal spaces
filtered_data.gsub(/secondaryPort = \d+/, 'secondaryPort = 7555')
=> 'secondaryPort = 7555'

# with regex character class for literal space
filtered_data.gsub(/secondaryPort\s{1}=\s{1}\d+/, 'secondaryPort = 7555')
=> 'secondaryPort = 7555'

并将其添加到您的Pref.xml文件中:

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);
    }
}

方法2: 一种简单的方法是在此处设置<ali.UI.Customize.MyPreferenceCategory android:title="@string/pref_server" /> 的自定义布局:

preferenceCategory

一种简单的方法是在此处为preferenceCategory设置自定义布局:

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

然后将代码设置在<PreferenceCategory android:layout="@layout/preferences_category" android:title="Privacy" > 布局文件中:

preferences_category