XML绘制对象不能扩大到视图的大小,或查看不继承其父身高正常,在Android API&LT; 11 [英] XML Drawable not expanded to View size, or View not inheriting its parent's height correctly, on Android API < 11
问题描述
考虑以下布局:
<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
仍然是完全独立的,这样我可以申请如补间动画的α它不会影响任何东西。
I'm using it to have bg2
on top of bg1
while bg2
remains completely independent, such that I can apply e.g. a tween alpha animation to it without affecting anything else.
BG1
和 BG2
是XML可绘制。显然,他们应该扩展到它们各自的视图的尺寸。这是正常的情况下,它似乎并没有多大意义,明确指定其尺寸。
bg1
and bg2
are XML drawables. Obviously, they're supposed to be scaled to their respective View's dimensions. This is normally the case and it doesn't seem to make much sense to specify their dimensions explicitly.
不幸的是,在Android上之前的版本3/11 API,它看起来好像 BG2
的规模将是零。也许两相布局测量越野车(注意如何 BG2
应该从它的母公司,而这又需要调整,以继承其高度的LinearLayout
的高度和传播这些信息包含视图 BG2
)。或者,也许视图将不接受其父母的身高(虽然我试过的ImageView
这什么也没有改变)。
Unfortunately, on Android versions prior to 3/API 11, it looks as if the size of bg2
will be zero. Maybe the two-phase layout measurement is buggy (note how bg2
is supposed to inherit its height from its parent, which in turn needs to adjust to the LinearLayout
´s height and propagate that information to the View containing bg2
). Or maybe the View won't accept its parent's height (although I tried ImageView
which changed nothing).
您看到的布局是真正用于列表中的项目。
The Layout you see is really used for items in a list.
XML的可绘制的状态和使用的渐变。
The XML drawables are stateful and use gradients.
你能想到这也为Android API 8工作到10的方法?
Can you think of an approach which would also work for Android API 8 to 10?
推荐答案
测试的位(子类化查看
,覆盖 onMeasure()
和 onLayout()
)显示,的FrameLayout
在这方面较旧的Android版本中被打破。
A bit of testing (subclassing View
, overriding onMeasure()
and onLayout()
) reveals that FrameLayout
is broken in this respect in older Android versions.
由于的FrameLayout
未能通过其自身的高度沿层次在这种情况下(即查看
总会看到 0
都与 onMeasure()
和 onLayout()
)有没有要解决这个问题,通过子类明显的方式。
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.
那么现在的问题是,有另一种方式来覆盖这两种观点哪的Android 2.2又名API 8可以正确处理。
The question then was, is there another way to overlay the two views which Android 2.2 aka API 8 can handle correctly.
唉,没有。同样可以实现一个 RelativeLayout的
,其中涉及当然更多的开销,但是在渲染工作的实际增长应该是有限的。
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>
这篇关于XML绘制对象不能扩大到视图的大小,或查看不继承其父身高正常,在Android API&LT; 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!