Android 更改首选项的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3551169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:44:12  来源:igfitidea点击:

change background color of Preference

androidandroid-emulator

提问by David Prun

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 to set the background of my settings screen, this screen is displayed with help of xml file shown below. Please see that I have already defined the android:background="#041A37", still the screen remains default color: black.

我有一个PreferenceCategory.xml 文件,并且在其中定义了所有首选项,我从扩展PreferenceActivity. 我无法设置我的设置屏幕的背景,这个屏幕是在下面显示的 xml 文件的帮助下显示的。请注意,我已经定义了android:background="#041A37",但屏幕仍然保持默认颜色:黑色。

public class MyPreferenceActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Context mContext=super.getBaseContext();
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.preference);
        //v.setBackgroundColor(Color.rgb(4, 26, 55));
    }
}


preference.xml is

首选项.xml 是

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#041A37" >

    <PreferenceCategory>
        <com.dropcall.SeekBarPreference
            android:background="#041A37"
            android:defaultValue="5"
            android:key="@string/Interference_Delay"
            android:progressDrawable="@drawable/seekbardrawable"
            android:title="Seconds Delay until intereference" />

        <com.dropcall.SeekBarPreference2
            android:defaultValue="30"
            android:key="@string/Drop_Delay"
            android:progressDrawable="@drawable/seekbardrawable"
            android:title="Seconds delay until drop" />

        <CheckBoxPreference
            android:background="@drawable/state_normal"
            android:defaultValue="true"
            android:key="@string/Drop_Option"
            android:title="Close after call drop" />
        <CheckBoxPreference
            android:background="@drawable/state_normal"
            android:defaultValue="true"
            android:key="@string/Timer_Option"
            android:title="Start timers on launch" />
    </PreferenceCategory>

</PreferenceScreen>


Although I have set android:background="#041A37"in every file, the background doesn't turn into navy blue, or any other color for that matter. It remains default color, black. How to change the background color. Please let me know any pointers / hints , if you had faced same issue let me know what changes you made to set the background color.

虽然我android:background="#041A37"在每个文件中都设置了背景,但背景不会变成海军蓝或任何其他颜色。它保持默认颜色,黑色。如何更改背景颜色。请让我知道任何指针/提示,如果您遇到同样的问题,请告诉我您为设置背景颜色所做的更改。

采纳答案by David Prun

This worked for me

这对我有用

getListView().setBackgroundColor(Color.TRANSPARENT);

getListView().setCacheColorHint(Color.TRANSPARENT);

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

回答by AndreasW

You can define a theme and then set this for your PreferenceActivity in the manifest. Your theme can then define a a background color or a windowBackground image should you prefer that.

您可以定义一个主题,然后在清单中为您的 PreferenceActivity 设置它。如果您愿意,您的主题然后可以定义背景颜色或 windowBackground 图像。

Manifest:

显现:

    <activity android:label="@string/app_label" android:name=".client.PreferencesActivity"
        android:theme="@style/PreferencesTheme">
        <intent-filter>                                
        </intent-filter>
    </activity>

Then add the theme to your styles.xml

然后将主题添加到你的styles.xml

<style name="PreferencesTheme">
    <item name="android:windowBackground">@drawable/background_image</item>
    <item name="android:background">#FFEAEAEA</item>
</style>

In the above snippet there's both a background color and a background image defined to show how to do it.

在上面的代码片段中,定义了背景颜色和背景图像来展示如何操作。

回答by Sileria

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>

回答by CommonsWare

android:backgroundis not an available attribute, according to the documentation.

android:background根据文档,不是可用属性。

It is possible you could theme the PreferenceActivityto achieve your color change, though I have not tried this, because I want my preferences to look like those of the rest of Android, to improve usability of the app.

您可以设置主题PreferenceActivity来实现您的颜色更改,尽管我还没有尝试过,因为我希望我的偏好看起来像 Android 其他部分的偏好,以提高应用程序的可用性。

回答by Rootko

Or you can also make drawable as your background:

或者你也可以将 drawable 作为你的背景:

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

获取列表视图()。setBackgroundDrawable(getResources().getDrawable(R.drawable.bluegradient));

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

注意:setBackgroundDrawable()已弃用。使用setBackground()来代替。

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

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

回答by madhavi

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

请指定一个带有文本视图的线性布局并指定背景颜色并使用布局属性将此 xml 附加到首选项类别。

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

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

其中 preftitle 是一个 xml,它具有线性布局和附加的文本视图。

回答by Avinash Ajay Pandey

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/activis color resources added to colors.

转到res>values>styles.xml>并将此代码添加到您<style > </style>的样式中,该样式必须是应用程序基础主题,这@color/activ是添加到颜色的颜色资源。

<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_themename of style tag then add like this to your manifest

如果您使用app_theme样式标签的名称,则将这样的添加到您的清单中

<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

如果你只想改变你的背景