更改首选项屏幕背景颜色

时间:2012-10-03 15:08:35

标签: android preferences preferenceactivity

我使用此代码更改颜色PreferenceScreen。但是如何在主要首选项活动中获取首选项屏幕并更改首选项屏幕颜色???

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

4 个答案:

答案 0 :(得分:7)

您可以覆盖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>

答案 1 :(得分:1)

只需扩展AppTheme并使用colorBackground属性

<style name="PreferenceTheme" parent="@style/AppTheme">
    <item name="android:colorBackground">@color/colorBackground</item>
</style>

答案 2 :(得分:0)

为您的活动使用Style

答案 3 :(得分:0)

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

SettingsScreen

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)
    }
}