0

我正在为我的偏好活动设置一个背景,所以我在 styles.xml 中编写了一个样式

<style 
   name="PreferencesTheme" 
   parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
   <item name="android:windowBackground">@drawable/background</item>
</style>

并且在活动中,

  <activity
          android:name="com.phonelight.realparrot.MainActivity"
          android:label="Real Parrot"
          android:screenOrientation="portrait"
          android:theme="@style/PreferencesTheme">
       <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>

  </activity>

我在两个设备的模拟器上运行它。第一个是手机,第二个是平板电脑。 手机 药片

为什么平板电脑上不显示背景图像。只有很小的余量。我在真正的平板电脑上运行它,整个屏幕也是白色的。

4

3 回答 3

0

我认为您想使用不同的主题。我不确定为什么两者之间的区别究竟是什么,但我怀疑 API 略有不同。

<style 
   name="PreferencesTheme" 
   parent="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
   <item name="android:windowBackground">@drawable/background</item>
</style>
于 2012-12-09T12:36:55.310 回答
0

我通过添加偏好活动来解决问题

preference.setBackgroundResource(R.xml.background);
于 2012-12-09T13:20:19.873 回答
0

我偶然发现了平板电脑的这个问题,但 yasserbn 的回答并没有帮助我,即没有迹象表明什么......

preference.setBackgroundResource(R.xml.background);

...正在被调用。

在 Android 大屏幕上,PreferenceActivity 会自行重启以在片段中显示首选项。出现问题是因为我们无法更改此片段容器的背景,因为它的布局 ID com.android.internal.R.id.prefs_frame 是私有的。

一种解决方法是递归清除所有子视图的背景,因为我们总是可以获取根视图:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ThemeManager.setTheme(this);
    super.onCreate(savedInstanceState);
    clearBackground(findViewById(android.R.id.content));

    // other stuff...
}

private void clearBackground(View view) {
    view.setBackgroundResource(0);
    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++)
            clearBackground(viewGroup.getChildAt(i));
    }
}

这并不完美,即如果您的任何偏好使用带有背景的自定义视图,尽管这不太可能。

于 2013-09-26T21:58:14.903 回答