XML Drawable not expanded to View size, or View not inheriting its parent's height correctly, on Android API < 11

微笑、不失礼 提交于 2019-11-29 15:44:15

A bit of testing (subclassing View, overriding onMeasure() and onLayout()) reveals that FrameLayout is broken in this respect in older Android versions.

Since FrameLayout fails to pass its own height down the hierarchy in this scenario (the View will always see 0 both with onMeasure() and onLayout()) there is no obvious way to work around this problem by subclassing.

The question then was, is there another way to overlay the two views which Android 2.2 aka API 8 can handle correctly.

And alas, there is. The same can be achieved with a RelativeLayout, which involves much more overhead of course, though the actual increase in rendering effort should be limited.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg1" >
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignTop="@+id/item"
        android:layout_alignBottom="@+id/item"
        android:background="@drawable/bg2" />
    <LinearLayout
        android:id="@+id/item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
</RelativeLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!