java - 如何更改偏好片段中偏好的文本颜色?
问题描述
我已经阅读了几十个线程,解释了如何在 PreferenceActivity 中更改首选项的文本颜色。我没有偏好活动。我在 res/xml 中使用 PreferenceFragment 和 PreferenceScreen
设置片段
public class SettingsFragment extends PreferenceFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences); // the settings XML here (not the layout)
}
}
片段设置.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/AppTheme2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
首选项.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="General">
<CheckBoxPreference
android:key="pref_example_checkbox"
android:title="first box"
android:summaryOn="This is enabled"
android:summaryOff="This is disabled"
android:defaultValue="false" />
</PreferenceCategory>
<PreferenceCategory
android:title="Notifications">
<SwitchPreference
android:key="pref_enable_notifications"
android:title="Notifications on/off"
android:defaultValue="true" />
<EditTextPreference
android:key="pref_notification_time"
android:title="Notification time"
android:summary="Some shit about notifications"
android:inputType="number"
android:dialogMessage="Dialog message!"
android:defaultValue="5" />
</PreferenceCategory>
<PreferenceCategory
android:title="About">
<Preference
android:key="pref_about_licenses"
android:title="Licenses" />
<Preference
android:key="pref_about_app_version"
android:title="Version" />
</PreferenceCategory>
</PreferenceScreen>
我尝试添加android:textColor="@color/white"
到 中的所有 Preference 项目preferences.xml
,但所有文本仍然是黑色的。所有这些线程都在谈论创建新样式,但没有一个解释如何将其应用于偏好片段。
偏好活动将显示在清单中,可以轻松应用样式。但是对于 a PreferenceFragment
,清单中没有条目。此外,似乎必须有一种比创建全新样式更快的方法将文本颜色应用于首选项屏幕。
解决方案
将此添加到主题
///偏好片段字体颜色
<item name="android:textColorPrimary">@color/white_font_color</item>
<item name="android:textColorSecondary">@color/nice_grey_dark</item>
<item name="android:textColorTertiary">@color/white_font_color</item>
推荐阅读
- firebase - 如何使用firebase云功能过滤firebase云存储并将数据发送到客户端
- python - Mlxtend 与 Sklearn 中不同的 roc_auc 分数
- node.js - 登录后jwt访问私人页面适用于邮递员,但不适用于ejs视图
- node.js - 识别/比较从数据库调用返回的信息的问题
- java - 带有过滤器的Android ListView:“打开”过滤列表的项目返回该位置的项目,但在原始列表中
- ios - 适用于 VPN 的 iOS Mobileconfig - iOS 将不接受代理设置
- python - Python 脚本没有被解释
- swift - 返回箭头作为 Swift 中的一种类型
- c++ - 使用 std::move 从 std::optional 缓冲区传递参数时指向空白空间的变量
- reactjs - 测试从本地 JSON 文件中获取一些数据的异步操作创建器