将视图添加到父级时,RelativeLayout 不会扩展以填充父级

RelativeLayout not expanding to fill parent when views are added to parent

我有一个 RelativeLayout inside a PercentRelativeLayout,如下面的代码所示

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                               xmlns:app="http://schemas.android.com/apk/res-auto"
                                               android:layout_width="match_parent"
                                               android:layout_height="wrap_content"
                                               android:orientation="vertical"
                                               android:padding="10dp">

    <RelativeLayout
        android:id="@+id/clickBox"
        android:layout_height="match_parent"
        android:background="@color/PaleBlack"
        android:fillViewport="true"
        app:layout_widthPercent="35%">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:scaleX="0.8"
            android:scaleY="0.8"
            android:src="@mipmap/ic_open"/>

    </RelativeLayout>

</android.support.percent.PercentRelativeLayout>

效果如下图,不错

但是如果我随后以编程方式将 TextView 添加到 PercentRelativeLayoutRelativeLayout 不会扩展并垂直填充父级 PercentRelativeLayout,结果如下所示

我该如何解决这个问题,让 RelativeLayout 填充父元素的高度?

由于 PercentRelativeLayout 已弃用,您可以考虑切换到具有如下布局的 ConstraintLayout 解决方案:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.30" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="false"
            android:layout_centerInParent="true"
            android:adjustViewBounds="false"
            app:srcCompat="@mipmap/ic_launcher" />

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

您可以添加第二条准则并将您生成的 TextView 锚定到准则和父级,就像我对 RelativeLayout 所做的那样。