【发布时间】:2013-04-13 01:01:20
【问题描述】:
在这里拉我的头发。因此,我正在使用具有多种类型的可绘制对象的应用程序,并且它们的结构如下:
res/
//Portrait resources
drawable-mdpi/
drawable-hdpi/
drawable-xhdpi/
//Landscape resources
drawable-land-mdpi/
drawable-land-hdpi/
drawable-land-xhdpi/
编辑我什至尝试将特定于肖像的可绘制对象放在drawable-port-mdpi 等下,结果相同。
我有一个自定义的View,我用它在运行时拉入drawables。这是一个精简的示例,但这是我遇到问题的地方:
public class CustomView extends FrameLayout {
private ImageView mBackgroundView;
public CustomView (Context context) {
super(context);
initialize();
}
public CustomView (Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public CustomView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
private void initialize () {
mBackgroundView = new ImageView(getContext());
mBackgroundView.setImageResource(R.drawable.my_resource);
addView(mBackgroundView, wrapContent());
}
private LayoutParams wrapContent () {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
}
我希望当我旋转到横向时(首先让我澄清一下,我不是在清单中为这个Activity处理configChanges)并且这个View是re-创建后,mBackgroundView 应该在drawable-land-xhdpi 中获取可绘制对象。相反,当我向后旋转时,我应该在drawable-xhdpi 中获得可绘制对象。
现在这里变得奇怪了。我可以确认所有可绘制对象都在那里并被检测到,因为如果我以横向方式启动应用程序,我会得到正确的横向可绘制对象。然而,在旋转时,风景画仍然存在,我没有得到肖像画。相反,如果我以纵向模式启动,我会卡在纵向可绘制对象上。
当我以编程方式获取资源时,是否应该处理一些特殊情况以“重置”资源或其他东西?
【问题讨论】:
-
什么版本的安卓?您是否启用了屏幕自动旋转?
-
@cdonner 是的,屏幕旋转,我得到了
layout-land中定义的布局。它只是不会改变的drawable。此外,这是在 Android 4.2 上(Nexus 4 和 Galaxy Nexus 都显示此问题。)
标签: android rotation android-activity android-resources android-drawable