如何将 preferences.xml 中的文字设为黑色,这样我就不会在白底上出现白色文字?

How to make text black in preferences.xml, so that I don't have white text on a white background?

我使用此 android developer guide 创建了设置菜单的基本实现。正如您从底部的 gif 中看到的那样,文本显示为白色,而我的背景为白色。有没有办法将主题设置为 light 之类的,以便文本可读?

非常感谢任何帮助。

Preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceScreen
        android:key="test_key"
        android:title="@string/button.preference.unit"
        android:persistent="true">
        <ListPreference
            android:key="test_list_key"
            android:defaultValue="1"
            android:entries="@array/testListArray"
            android:entryValues="@array/testListValues" />
    </PreferenceScreen>
</PreferenceScreen>

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_settings);

        initToolbar();

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new SettingsFragment())
                .commit();
    }

    private void initToolbar() {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);
        }
    }
}

activity_settings.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/toolbar"/>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

当然,您可以像这样在应用程序启动时强制使用主题,Android 开发者网站上的 Apply a theme to an Activity or application

下对此进行了记录

如文档中所述,您可以在 activity xml 页面的 activity 级别强制使用预设主题:

<activity android:theme="@android:style/Theme.Light">

或AndroidManifest.xml中的应用级别:

<application android:theme="@android:style/Theme.Light">

如果更有意义,您可以随时在自定义主题中挑选要覆盖的属性:

<style name="CustomTheme" parent="android:Theme.Light">
    <item name="textColorPrimary">@color/primary_text_light</item>
</style>

...并酌情引用。作为示例提供的应用程序级别:

<application android:theme="@style/CustomTheme">

可以在链接文档的末尾找到所有可用样式和主题的名称和值的完整参考