【问题标题】:Custom Android preferences background自定义 Android 首选项背景
【发布时间】:2013-03-15 10:35:27
【问题描述】:

我目前正在尝试为首选项屏幕设置自定义背景。我创建了PreferenceActivity

public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
}

这是我的偏好布局

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    >
    <CheckBoxPreference 
        android:key="pref_Autoplay"
        android:title="@string/pref_Autoplay"
        android:summary="@string/pref_Autoplay_summ"
        android:defaultValue="false"/>
    <ListPreference 
        android:entryValues="@array/pref_Voice_values" 
        android:entries="@array/pref_Voice_entries" 
        android:key="pref_Voice" 
        android:summary="Choose male or female reader" 
        android:title="Readers voice" 
        android:defaultValue="female"/>


</PreferenceScreen>

当我在这里阅读以更改背景时,我需要创建自己的风格主题。这里是:

<style name="PrefsTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@drawable/prefs_bg</item>
        <item name="android:textColor">@color/prefs_bg</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>
        <item name="android:textColor">@color/prefs_bg</item>
    </style>

我只需要为首选项列表设置一些图像背景。我的头像是@drawable/prefs_bg。当我将windowBackground 设置为图像并将background 设置为某种颜色时,首选项列表背景使用颜色而不是图片。但是当我将背景设置为@drawable/prefs_bg 时,首选项列表背景与我的图片一起出现,而我的ListPreference 项目的单选按钮对话框也使用图像背景。

请帮忙,如何在不影响某些首选项的单选按钮对话框背景的情况下为首选项列表设置图像背景?

【问题讨论】:

    标签: android background customization preferenceactivity


    【解决方案1】:

    好的。我找到了如何解决我的问题。这是我的错。设置偏好背景只需要

    <item name="android:windowBackground">@drawable/prefs_bg</item>
    

    这个是不必要的:

    <item name="android:background">@color/prefs_bg</item>
    <item name="android:cacheColorHint">@color/prefs_bg</item>
    

    【讨论】:

      【解决方案2】:

      要添加动态性质,这也可以。将此添加到 PreferenceActivity。我们也可以用同样的方法设置背景颜色。

      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
          super.onPostCreate(savedInstanceState);
          setupActionBar();
          setupSimplePreferencesScreen();
      
          if (Build.VERSION.SDK_INT >= 16)
              this.getListView().setBackground(getResources().getDrawable(...);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-13
        • 2011-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多