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

后端 未结 1 2026
轻奢々
轻奢々 2020-12-21 15:30

Consider the following layout:



        
相关标签:
1条回答
  • 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>
    
    0 讨论(0)
提交回复
热议问题