【问题标题】:Drawables pulled from wrong resource folder on rotation旋转时从错误的资源文件夹中提取的可绘制对象
【发布时间】: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


【解决方案1】:

所以我已经弄清楚问题出在哪里,老实说,我不确定这是我的误解还是 Android 中的错误。我不认为将其添加为问题的一部分(回头看,我应该有),但我的可绘制对象是可绘制的图层列表,其中两个图像相互堆叠(一个阴影,一个阴影上方的图像):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android>
    <item>
        <bitmap android:src="@drawable/shadow"/>
    </item>

    <item>
        <bitmap android:src="@drawable/image" android:gravity="top|center_horizontal"/>
    </item>
</layer-list>

这似乎只被解析了一次(我假设它被重复使用)但它永远不会返回并使用新配置的可绘制对象重建层列表。 p>

我改为使用两个对齐的 ImageViews 而不是 layer-list drawable,一切都很好。

【讨论】:

  • 我刚刚发现我有同样的问题,我在所有活动的样式中都使用了这个,背景颜色和位图的形状,但它似乎取决于它开始的位置它不会适应轮换。你能用imageviews显示你的xml吗?这可以在样式主题中使用吗?
猜你喜欢
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多