change background color of Preference

后端 未结 8 1777
北荒
北荒 2020-12-02 10:25

I have a PreferenceCategory, xml file and I have defined all preferences in it, I call this from class that extends PreferenceActivity. I am unable

相关标签:
8条回答
  • 2020-12-02 10:51

    Please specify a linear layout with a textview attached and specify background color and attach this xml to preferencecategory using the layout property.

    <PreferenceCategory
         android:layout="@layout/preftitle"
     >
    

    Where preftitle is an xml which has a linear layout and text view attached.

    0 讨论(0)
  • 2020-12-02 10:53

    This worked for me

    getListView().setBackgroundColor(Color.TRANSPARENT);
    
    getListView().setCacheColorHint(Color.TRANSPARENT);
    
    getListView().setBackgroundColor(Color.rgb(4, 26, 55));
    
    0 讨论(0)
  • 2020-12-02 10:58

    I am using the PreferenceFragmentCompat, the below solution worked for me.

    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)
    }
    }
    
    0 讨论(0)
  • 2020-12-02 11:00

    Or you can also make drawable as your background:

    getListView().setBackgroundDrawable(getResources().getDrawable(R.drawable.bluegradient));

    Note: setBackgroundDrawable() is deprecated. Use setBackground() instead.

    getListView().setBackground(getResources().getDrawable(R.drawable.bluegradient));

    0 讨论(0)
  • 2020-12-02 11:07

    Go to res>values>styles.xml> and add this code to your <style > </style> the style should must be app base theme in this @color/activ is color resources added to colors.

    <style name="app_theme" parent="@android:style/Theme">
        <item name="android:windowBackground">@color/activ</item>
        <item name="android:windowContentOverlay">@drawable/title_bar_shadow</item>
        <item name="android:listViewStyle">@style/TransparentListView</item>
    </style>
    

    if you use app_theme name of style tag then add like this to your manifest

    <application
        android:name=".XXXXXX"
        android:allowBackup="true"
    
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/app_theme" > //here
    

    If you only want to change your background

    0 讨论(0)
  • 2020-12-02 11:09

    Another work-around as far as color goes is that you create a theme for the preferences activity and put the background color of list views as well:

    <style name="PrefsTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@color/prefs_bg</item>
        <item name="android:textColor">@color/text_color</item>
        <item name="android:listViewStyle">@style/listViewPrefs</item>
    </style>
    
    <style name="listViewPrefs" parent="@android:style/Widget.ListView">
        <item name="android:background">@color/prefs_bg</item>
        <item name="android:cacheColorHint">@color/prefs_bg</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题