Android 白色背景显示对平板电脑的偏好
我正在为我的首选项活动设置背景,所以我在styles.xml中编写了一个样式Android 白色背景显示对平板电脑的偏好,android,background,preferenceactivity,android-theme,Android,Background,Preferenceactivity,Android Theme,我正在为我的首选项活动设置背景,所以我在styles.xml中编写了一个样式 <style name="PreferencesTheme" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen"> <item name="android:windowBackground">@drawable/background</item> </style> @可绘制/背景
<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>
我在两个设备的模拟器上运行这个。第一个是手机,第二个是平板电脑。
为什么平板电脑上不显示背景图像。差距很小。我在真正的平板电脑上运行它,整个屏幕也是白色的。我想你应该使用不同的主题。我不确定这两者之间的区别到底是什么,但我怀疑API中有细微的区别
<style
name="PreferencesTheme"
parent="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
<item name="android:windowBackground">@drawable/background</item>
</style>
@可绘制/背景
我通过添加首选项活动来解决问题
preference.setBackgroundResource(R.xml.background);
我在平板电脑上偶然发现了这个问题,但亚塞文的回答对我没有帮助,也就是说,没有迹象表明
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));
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
themanager.setTheme(this);
super.onCreate(savedInstanceState);
clearBackground(findviewbyd(android.R.id.content));
//其他东西。。。
}
私有void clearBackground(视图){
查看。setBackgroundResource(0);
if(视图组的视图实例){
ViewGroup ViewGroup=(ViewGroup)视图;
对于(int i=0;i
这并不完美,也就是说,如果您的任何偏好使用带有背景的自定义视图,尽管这无论如何都不太可能。这两种API的版本是什么?手机是Android 2.2;平板电脑是安卓3.0你认为在主题中设置背景对平板电脑上的偏好活动不起作用吗。也许我应该在java代码中设置首选项活动的背景,你认为呢?它不起作用。我通过添加java代码来解决这个问题:preference.setBackgroundResource(R.xml.background);不过,我很感谢你的帮助。如果你能为你的问题发布一个答案,这将帮助那些后来偶然发现同样问题的人,我将不胜感激。