更改首选项的背景颜色

Dav*_*run 52 android android-emulator

我有一个PreferenceCategoryxml文件,我已经在其中定义了所有首选项,我从扩展的类中调用它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));
    }
}
Run Code Online (Sandbox Code Playgroud)

preference.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>
Run Code Online (Sandbox Code Playgroud)

虽然我已经设置android:background="#041A37"了每个文件,但背景并没有变成深蓝色或任何其他颜色.它仍然是默认颜色,黑色.如何更改背景颜色.请告诉我任何指示/提示,如果您遇到同样的问题,请告诉我您为设置背景颜色所做的更改.

And*_*asW 78

您可以定义主题,然后在清单中为PreferenceActivity设置此主题.然后,您可以根据需要定义背景颜色或windowBackground图像.

表现:

    <activity android:label="@string/app_label" android:name=".client.PreferencesActivity"
        android:theme="@style/PreferencesTheme">
        <intent-filter>                                
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

然后将主题添加到styles.xml中

<style name="PreferencesTheme">
    <item name="android:windowBackground">@drawable/background_image</item>
    <item name="android:background">#FFEAEAEA</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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

  • 如果你只想改变背景颜色,我建议设置一个定义颜色的`android:windowBackground`,比如`@color/myFavoriteColor`,否则会覆盖每个视图的背景(图像),包括活动的对话框. (7认同)

Dav*_*run 44

这对我有用

getListView().setBackgroundColor(Color.TRANSPARENT);

getListView().setCacheColorHint(Color.TRANSPARENT);

getListView().setBackgroundColor(Color.rgb(4, 26, 55));
Run Code Online (Sandbox Code Playgroud)

  • 你为什么两次调用setBackgroundColor? (19认同)

Sil*_*ria 21

颜色的另一个解决方法是为首选项活动创建主题并将列表视图的背景颜色也添加到:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 很好的答案!特别是指出了一种在PrefereneScreen中为ListView设置cacheColorHint值的方法! (3认同)

Com*_*are 6

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

PreferenceActivity虽然我没有尝试过这个,但我可能会主题化以实现你的颜色变化,因为我希望我的偏好看起来像Android的其他部分,以提高应用程序的可用性.

  • "我希望我的偏好看起来像Android的其他部分" - 我不认为这是一个有蜂窝的有效点,因为Android偏好应用程序本身现在使用自定义背景. (4认同)

小智 5

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

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

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

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