【问题标题】:ViewStub ignors alignParentBottom property in RelativeLayoutViewStub 忽略 RelativeLayout 中的对齐父底部属性
【发布时间】: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


    【解决方案1】:

    对不起兄弟,我只是误解了你的意思。

    如果您看到 Offical Document ,您可能会看到这条消息“A ViewStub 是一个不可见的、大小为零的视图”。让我们看看 ViewStub 中的一些源代码:

    private void initialize(Context context) {
        mContext = context;
        setVisibility(GONE);
        setWillNotDraw(true);
    }
    

    ViewStub 初始化时,它设置为GONE,这就是为什么你的android:layout_alignParentBottom="true"RelativeLayout 中无效。

    您可以尝试使用“简单视图”而不是ViewStub,并将该视图visibility 设置为GONEandroid:layout_alignParentBottom="true" 也无效。

    所以,我不认为这是一个错误!

    编辑: 我发现一个类似的问题:Issue with RelativeLayout when View visibility is View.GONE

    您可以在“Some text2” TextView 中添加以下代码,一切都会解决! android:layout_alignWithParentIfMissing="true"

    【讨论】:

    • 是的,如果 ViewStub 被膨胀,你会得到这个结果。但就我而言,我只在极少数情况下才需要充气。
    • 嘿兄弟,我只是误解并更新了答案。
    • 非常感谢,上帝保佑你)
    猜你喜欢
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2012-11-06
    • 2011-06-08
    • 2012-12-13
    相关资源
    最近更新 更多