Android ViewStub ignors RelativeLayout中的alignParentBottom属性
我在RelativeLayout中遇到了奇怪的ViewStub行为:Android ViewStub ignors RelativeLayout中的alignParentBottom属性,android,Android,我在RelativeLayout中遇到了奇怪的ViewStub行为: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match
<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上遗漏了什么
例如,如果我将用简单视图和相同的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">
<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打电话。如果在ViewStub上调用了充气方法,那么一切都会很完美。对不起,兄弟,我只是误解了你的意思 如果看到,您可能会看到以下消息“ViewStub是一个不可见的零大小视图”。让我们看看ViewStub中的一些源代码:
private void initialize(Context context) {
mContext = context;
setVisibility(GONE);
setWillNotDraw(true);
}
当ViewStub初始化时,它被设置为GONE
,这就是为什么您的android:layout\u alignParentBottom=“true”
在RelativeLayout
中无效的原因
您可以尝试使用“简单视图”而不是ViewStub
,并将该视图的可见性设置为GONE
android:layout\u alignParentBottom=“true”
也无效
所以,我不认为这是一个错误
编辑:
我发现了一个类似的问题:
您可以在“Some text2”文本视图中添加以下代码,所有问题都可以解决!
android:layout\u alignWithParentIfMissing=“true”
是的,如果ViewStub膨胀,您将得到此结果。但在我的情况下,我只需要在极少数情况下夸大它。嘿,兄弟,我只是误解了答案并更新了答案。非常感谢,上帝保佑你)