在旋转时从错误的资源文件夹中拉出Drawables

时间:2013-04-13 01:01:21

标签: android rotation android-activity android-resources android-drawable

把头发拉出来。所以我正在使用一个具有多种类型drawable的应用程序,我将它们放在这样的结构中:

res/
    //Portrait resources
    drawable-mdpi/
    drawable-hdpi/
    drawable-xhdpi/

    //Landscape resources
    drawable-land-mdpi/
    drawable-land-hdpi/
    drawable-land-xhdpi/

编辑我甚至尝试将特定于肖像的drawable放在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);
    }
}   

我希望当我转向横向广告时(让我首先澄清一下,我 AM NOT 处理此configChanges的清单中的Activity)和此{{ 1}}被重新创建,View应该在mBackgroundView中得到drawable。相反,当我向后旋转时,我应该在drawable-land-xhdpi中得到drawable。

现在这里变得奇怪了。我可以确认drawables都在那里并被检测到,因为如果我在横向上启动应用程序,我会得到正确的横向绘图。然而,在旋转时,景观可绘制仍然存在,而且我没有得到肖像。相反,如果我以肖像方式发布,我就会被纵向绘制。

当我以编程方式抓取资源时,我是否应该处理一些“重置”资源的特殊情况?

1 个答案:

答案 0 :(得分:1)

所以我已经弄清楚问题所在的位置,而且老实说我不确定这是我的误解还是Android中的错误。我不认为这是问题的一部分(回头看,我应该有),但我的drawable是一个图层列表drawable与两个图像堆叠在一起(一个阴影,阴影上方的一个图像):

<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>

似乎这只会被解析出来一次(我假设效率已被重复使用)但它永远不会返回并使用新配置的drawable重建图层列表。

我改为使用两个对齐的ImageView而不是一个图层列表drawable,一切都很好。