6

我使用此代码更改颜色 PreferenceScreen。但是如何在主 Preference 活动中获取 Preference Screen 并更改 Preference Screen 颜色?

getListView().setBackgroundColor(Color.TRANSPARENT);
    getListView().setCacheColorHint(Color.TRANSPARENT);
    getListView().setBackgroundColor(Color.rgb(4, 26, 55));
4

4 回答 4

13

您可以覆盖 OnCreate 方法:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(<COLOR>));
        return view;
    }

或者你可以在styles.xml中输入以下内容

<style name="PreferenceTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
        <item name="android:background">@android:color/white</item>
    </style>
于 2016-03-25T04:09:48.843 回答
1

只需扩展 AppTheme 并使用 colorBackground 属性

<style name="PreferenceTheme" parent="@style/AppTheme">
    <item name="android:colorBackground">@color/colorBackground</item>
</style>
于 2019-01-16T12:36:24.233 回答
1

我正在使用 PreferenceFragmentCompat,以下解决方案对我有用。

设置画面

import android.os.Bundle
import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorInt
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.almarai.easypick.R


class SettingsScreen : PreferenceFragmentCompat(), 
Preference.OnPreferenceChangeListener {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

    //Providing the XML file for the view to be created
    setPreferencesFromResource(R.xml.app_settings_preferences, rootKey)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    //Get the Theme specific color
    val typedValue = TypedValue()
    val theme = requireContext().theme

    /*R.attr.colorBackgroundScreenBody is my own attr from attrs.xml file, 
    you can directly use R.color.red - Or any color from your colors.xml 
    file if you do not have multi-theme app.*/
    theme.resolveAttribute(R.attr.colorBackgroundScreenBody, typedValue, true)
    @ColorInt val color = typedValue.data

    view.setBackgroundColor(color)

    super.onViewCreated(view, savedInstanceState)
    }
}
于 2020-06-08T07:31:47.147 回答
0

为您的活动使用样式

于 2012-10-03T15:13:49.863 回答