如何在Android中更改设置片段的文本颜色?

时间:2018-12-05 11:41:48

标签: java android android-layout android-fragments

我想为我的应用程序设置暗模式,我唯一的问题是我无法更改片段中的文本颜色,用户可以打开/关闭暗模式。 这就是为什么我无法在清单中更改主题的任何解决方案?

if(!DarkTheme()) {
            setTheme(R.style.AppTheme);
        } else {
            setTheme(R.style.DarkAppTheme);
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.blacklight, null)));
            View fragment = (View) findViewById(R.id.settings_fragment);
            fragment.setBackgroundColor(getResources().getColor(R.color.Dark, null));
        }

这是用来检测是否打开了暗模式,背景颜色是否已全部更改,但我不知道如何更改片段中的文本颜色。

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <PreferenceCategory
        app:iconSpaceReserved="false"
        android:key="PREFS"
        android:title="Main settings (BETA)">

        <SwitchPreference
            android:key="darktheme"
            android:id="@+id/DarkTheme"
            app:iconSpaceReserved="false"
            android:title="Dark Theme"
            android:summary="Enable dark theme (BETA)"
            android:defaultValue="false" />

    </PreferenceCategory>

    <PreferenceCategory
        app:iconSpaceReserved="false"
        android:key="DOWN"
        android:title="Download settings">

        <CheckBoxPreference
            android:id="@+id/MobileData"
            android:key="mobiledata"
            android:title="Mobiele data"
            android:icon="@drawable/ic_network_cell_blue_24dp"
            android:summary="Download de voicemails op mobiele data"
            android:defaultValue="true"/>

    </PreferenceCategory>

    <PreferenceCategory
        app:iconSpaceReserved="false"
        android:key="Other"
        android:title="Other">

        <Preference android:title="Boodschap opnemen"
            android:id="@+id/Boodschap"
            android:key="@string/setVoicemail"
            android:summary="Spreek je eigen voicemail boodschap in">

            <intent
                android:action="android.intent.action.MAIN"
                android:targetPackage="com.tismi.voozzle_main"
                android:targetClass="com.tismi.voozzle_main.Popup" />

        </Preference>

        <Preference android:title="App uitleg"
            android:id="@+id/AppUitleg"
            android:key="AppUitleg"
            android:summary="Loop de uitleg van de app nog een keer door">

            <intent
                android:action="android.intent.action.MAIN"
                android:targetPackage="com.tismi.voozzle_main"
                android:targetClass="com.tismi.voozzle_main.Reopen_Expanation" />

        </Preference>

        <Preference android:title="Voozzle uitschakelen"
            android:key="voozzle_off"
            android:id="@+id/VoozzleOff"
            android:summary="Door op deze knop te klikken schakel je voozzle uit">

            <intent
                android:action="android.intent.action.MAIN"
                android:targetPackage="com.tismi.voozzle_main"
                android:targetClass="com.tismi.voozzle_main.tasks.TurnoffTask" />

        </Preference>

    </PreferenceCategory>

</PreferenceScreen>

这是我的设置页面(片段),我想更改标题和摘要的颜色。

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimaryLight</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowBackground">@color/white</item>
        <item name="panelBackground">@color/white</item>
    </style>

    <style name="DarkAppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/Dark</item>
        <item name="colorPrimaryDark">@color/Dark</item>
        <item name="colorAccent">@color/white</item>
        <item name="android:windowBackground">@color/Dark</item>
        <item name="panelBackground">@color/Dark</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:textColorSecondary">@color/white</item>
        <item name="android:textColorTertiary">@color/white</item>
    </style>

2 个答案:

答案 0 :(得分:0)

尝试使用ContextThemeWrapper设置主题。

final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.PreferenceScreen);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

在styles.xml中定义主题。

<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
    <item name="android:textColor">@color/white</item>
</style>

答案 1 :(得分:0)

好吧,我知道了,这很简单

getContext().getTheme().applyStyle(R.style.PreferenceScreen, true);

这只是将样式应用于活动,谢谢您的帮助!