【问题标题】:Child of RelativeLayout is not filling the height of the RelativeLayoutRelativeLayout 的子级没有填充 RelativeLayout 的高度
【发布时间】:2013-04-13 21:14:35
【问题描述】:

我有一个 ListView,其中填充了 RelativeLayouts。这些相对布局的内容可以改变,从而分别改变它们的高度。在每个 RelativeLayout 上,我在布局的左侧都有一个与布局高度匹配的小竖线。

和这个很像 -> Link

尽管出于某种原因,我正在使用的 View 对象没有扩展到布局的高度。

我做错了什么?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/post"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/column_post_selector"
android:orientation="horizontal" >

<!-- Status bar (THIS ISN'T FILLING TO PARENTS' HEIGHT) -->
<View
    android:id="@+id/status"
    android:layout_width="3dp"
    android:layout_height="fill_parent"
    android:layout_alignLeft="@id/post"
    android:layout_marginBottom="2dp"
    android:layout_marginTop="2dp"
    android:background="#33B5E5" />

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_toRightOf="@id/status"
    android:contentDescription="@string/empty"
    android:layout_marginRight="5dp"
    android:scaleType="fitStart" />

<!-- Title -->

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/post"
    android:layout_toRightOf="@id/thumbnail"
    android:textColor="#ffffff"
    android:textSize="14sp"
    android:typeface="sans" />
<!-- Rightend Duration -->

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/title"
    android:layout_marginRight="5dip"
    android:gravity="right"
    android:textColor="#10bcc9"
    android:textSize="12sp"
    android:textStyle="bold" />

</RelativeLayout>

【问题讨论】:

    标签: android layout fill-parent


    【解决方案1】:

    我在 RecyclerView 的单元格中遇到了类似的问题,该单元格有一个 RelativeLayout 作为根元素。

    我的一个解决方法是在根 RelativeLayout 元素周围包裹一个额外的 LinearLayout。

    所以你的卡片看起来像这样:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
            <RelativeLayout 
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                <View
                    android:id="@+id/view_with_same_height_as_parent"
                    android:layout_width="10dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentTop="true"/>
    ...
    
    
        </RelativeLayout>
    </LinearLayout>
    

    来源: https://stackoverflow.com/a/28799251/3708094

    【讨论】:

    • layout_alignParentBottom 和 layout_alignParentTop 参数是多余的,但您的建议非常有帮助。谢谢!
    • 这是唯一对我有用的解决方案。有趣的是,使用FrameLayout 不起作用,但使用LinearLayout 确实有效。
    【解决方案2】:

    如果您的 RelativeLayout 在 ScrollView 内,则需要在 ScrollView 上添加 fillViewPort 参数并将其设置为 true

      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fillViewport="true">
    

    【讨论】:

    • 为我工作!!
    • 这是我的问题!
    【解决方案3】:

    ScrollView 中的 RelativeLayout 也有类似的问题。 According to the documentation RelativeLayout(最高版本 17)中有一个错误,如果将其添加到滚动容器(如 ListView)中,会导致测量高度错误。

    所以要解决这个问题,你应该以编程方式计算高度。

    您可以创建扩展类并覆盖 onMeasure 方法或将 OnGlobalLayoutListener 添加到父视图。

    我选择第二种方式。这是我的解决方案:

    findViewById(R.id.scroll_view).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            float topElementPosition = findViewById(R.id.top_element).getY();
            float bottomElementPosition = findViewById(R.id.bottom_element).getY();
            int bottomElementHeight = findViewById(R.id.bottom_element).getMeasuredHeight();
            int height = (int)(bottomElementPosition-topElementPosition+bottomElementHeight);
            findViewById(R.id.view).setMinimumHeight(height);
            findViewById(R.id.scroll_view).getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });
    

    【讨论】:

      【解决方案4】:

      遇到了同样令人讨厌的问题,顺便说一句,这毫无意义。最后对我有用的是像这样将RelativeLayout标签中的android:layout_height更改为fill_parent

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" 
        ...
      

      希望对你有帮助

      【讨论】:

        【解决方案5】:

        我在onLayout 期间通过手动设置布局修复了无效视图:

        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
        
            int rootMarginRight = ((MarginLayoutParams)root.getLayoutParams()).rightMargin;
            int paddingRight = root.getPaddingRight() + rootMarginRight;
        
            //invalid view
            actionsContainer.layout(
                    right - actions.getWidth() - paddingRight,
                    0,
                    right - paddingRight,
                    getHeight()
            );
        }
        

        【讨论】:

          【解决方案6】:

          如果您知道要设置的确切高度,则可以为此组件(视图)设置 Height 或 MinHeight。在您的情况下,您可以设置为:android:minHeight="76dp",或者您可以使用 LayoutParams 以编程方式设置它。希望对您有所帮助!

          【讨论】:

            【解决方案7】:

            我的问题是下面一行

            android:fitsSystemWindows="true" <-- In the parent view
            

            已被移除。添加回来后,它现在可以正常工作了。

            【讨论】:

              【解决方案8】:

              您需要将layout_alignParentTop="true" 和layout_alignParentBottom="true" 添加到status - 这将确保视图触及post 的RelativeLayout 的顶部和底部。

              【讨论】:

              • 还是不行。非常奇怪,如果我将宽度设置为填充父级,它工作正常,但设置高度以填充父级似乎没有响应。
              • 也不要为我工作。但在我的情况下,孩子的高度等于它的内容,并附在父母的底部。
              • 可能不是完全正确,但一个很好的起点...layout_alignParentTop="true" 和layout_alignBottom="@id/sibling_element_which_dictates_size_of_parent" 对我有用:-)
              猜你喜欢
              • 1970-01-01
              • 2017-11-01
              • 2015-01-09
              • 1970-01-01
              • 1970-01-01
              • 2011-08-23
              • 1970-01-01
              • 2011-10-30
              • 1970-01-01
              相关资源
              最近更新 更多