【发布时间】:2015-05-13 01:44:51
【问题描述】:
我在 RelativeLayout 中遇到了奇怪的 ViewStub 行为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewStub
android:id="@+id/stub"
android:inflatedId="@+id/stub"
android:layout="@layout/some_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:text="Some text"
android:layout_height="wrap_content"/>
<TextView
android:layout_above="@id/stub"
android:layout_width="match_parent"
android:text="Some text2"
android:layout_height="wrap_content"/>
</RelativeLayout>
当在上面膨胀布局时,viewstub 似乎与父级顶部对齐,而不是底部对齐。 我也尝试将 ViewStub 的高度和宽度设置为固定值(dp 或 px),但得到了相同的结果。那么它是某种 Android 错误还是我错过了 ViewStub 的某些内容?
例如,如果我将 ViewStub 替换为简单的 View 和相同的 RelativeLayout 属性,则所有视图都以正确的方式插入:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/stub"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:text="Some text"
android:layout_height="wrap_content"/>
<TextView
android:layout_above="@id/stub"
android:layout_width="match_parent"
android:text="Some text2"
android:layout_height="wrap_content"/>
</RelativeLayout>
编辑: 我没有在 ViewStub 上调用 inflate。如果在 ViewStub 上调用 inflate 方法一切正常。
【问题讨论】:
标签: android