如何在preferences.xml中使文本变黑,这样我在白色背景上没有白色文字?

时间:2015-10-06 17:49:55

标签: android xml android-preferences

我使用此android developer guide创建了设置菜单的基本实现。正如你从底部的gif看到的那样,文本显示为白色,我有一个白色背景。有没有办法将主题设置为浅色或其他东西,以便文本可读?

非常感谢任何帮助。

的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>

enter image description here

1 个答案:

答案 0 :(得分:1)

当然,您可以像这样在app开始时强制使用主题,这在Android开发者网站Apply a theme to an Activity or application

下有记录。

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

<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">

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