考虑以下布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:background="@drawable/bg2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- some views here -->
</LinearLayout>
</FrameLayout>
我用它来让
bg2
位于bg1
之上,而bg2
保持完全独立,这样我就可以对它应用tween alpha动画,而不影响其他任何东西。bg1
和bg2
是XML可绘制的。显然,它们应该按各自视图的尺寸缩放。通常情况下是这样,显式地指定它们的维度似乎没有多大意义。不幸的是,在3/api 11之前的android版本上,
bg2
的大小似乎为零。也许两相布局测量是错误的(注意bg2
应该如何从其父级继承其高度,而父级又需要调整到LinearLayout
的高度并将该信息传播到包含bg2
的视图)。或者视图可能不接受其父视图的高度(尽管我尝试了ImageView
,但没有改变)。您看到的布局实际上用于列表中的项。
xml drawables是有状态的,使用渐变。
你能想出一种同样适用于android api 8到10的方法吗?
最佳答案
一些测试(子类化View
、重写onMeasure()
和onLayout()
)表明,在较旧的android版本中,FrameLayout
在这方面被破坏了。
由于在这个场景中,FrameLayout
无法在层次结构中传递它自己的高度(使用View
和0
时,onMeasure()
将始终看到onLayout()
),因此没有明显的方法通过子类化来解决这个问题。
接下来的问题是,有没有其他方法可以覆盖android 2.2aka api 8可以正确处理的两个视图。
唉,是的。使用RelativeLayout
也可以实现同样的效果,当然这需要更多的开销,不过渲染工作的实际增加应该是有限的。
<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>