Android 百分比相对长度与高度包裹内容和子项匹配父项 我正在使用PercentRelativeLayout,其中包含多个孩子 我希望一些孩子的身高和父母一样 父级的高度应为“包裹内容”

Android 百分比相对长度与高度包裹内容和子项匹配父项 我正在使用PercentRelativeLayout,其中包含多个孩子 我希望一些孩子的身高和父母一样 父级的高度应为“包裹内容”,android,layout,percentage,Android,Layout,Percentage,下面是一个过于简单的示例,但不起作用: <android.support.percent.PercentRelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/red" > <TextView android:id="@+id/text1"

下面是一个过于简单的示例,但不起作用:

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/red"
    >
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Label1 \n Line2 \n Line3"
        android:background="@color/green"
        />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/blue"
        android:layout_alignParentRight="true"
        android:text="Label2 with\n two lines"/>
</android.support.percent.PercentRelativeLayout>


解决此问题的最简单方法是什么?

作为一种解决方法,我做了以下工作,请评论是否可以这样做(在onMeasure中更改儿童身高)

public类PercentageRelativeLayout扩展了PercentRelativeLayout
{
公共PercentageRelativeLayout(上下文){
超级(上下文);
}
公共PercentageRelativeLayout(上下文、属性集属性){
超级(上下文,attrs);
}
公共PercentageRelativeLayout(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
if(getLayoutParams().height==LayoutParams.WRAP_CONTENT){
ArrayList modifiedLayouts=新建ArrayList();
对于(int i=0;i0){
int heightHint=getMeasuredHeight();
如果(高度提示>0){
getLayoutParams().height=heightHint;
}
对于(ViewGroup.LayoutParams参数:modifiedLayouts)
params.height=布局params.MATCH_父项;
超级测量(宽度测量、高度测量);
返回;
}
}
超级测量(宽度测量、高度测量);
}
}

作为一种解决方法,我做了以下工作,请评论是否可以这样做(在onMeasure中更改儿童身高)

public类PercentageRelativeLayout扩展了PercentRelativeLayout
{
公共PercentageRelativeLayout(上下文){
超级(上下文);
}
公共PercentageRelativeLayout(上下文、属性集属性){
超级(上下文,attrs);
}
公共PercentageRelativeLayout(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
if(getLayoutParams().height==LayoutParams.WRAP_CONTENT){
ArrayList modifiedLayouts=新建ArrayList();
对于(int i=0;i0){
int heightHint=getMeasuredHeight();
如果(高度提示>0){
getLayoutParams().height=heightHint;
}
对于(ViewGroup.LayoutParams参数:modifiedLayouts)
params.height=布局params.MATCH_父项;
超级测量(宽度测量、高度测量);
返回;
}
}
超级测量(宽度测量、高度测量);
}
}

因此,如果我理解正确,您要做的是将内部视图更改为包裹内容,重新计算高度,将高度指定给父级,然后再次更改内部视图以匹配父级?你为什么不改变布局内部视图来包装内容?@Kazie-因为我希望所有视图都与父视图匹配。例如,如果我有一个文本视图有两行文本,其他视图有一行文本。我的代码将使所有文本视图的高度为2行高度。因此,如果我理解正确,您要做的是将内部视图更改为包裹内容,重新计算高度,将高度指定给父视图,然后再次更改内部视图以匹配父视图?你为什么不改变布局内部视图来包装内容?@Kazie-因为我希望所有视图都与父视图匹配。例如,如果我有一个文本视图有两行文本,其他视图有一行文本。我的代码将使所有文本视图的高度为2行高度。
public class PercentageRelativeLayout extends PercentRelativeLayout
{
public PercentageRelativeLayout(Context context) {
    super(context);
}

public PercentageRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public PercentageRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
        ArrayList<ViewGroup.LayoutParams> modifiedLayouts = new ArrayList<>();
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child.getLayoutParams().height == LayoutParams.MATCH_PARENT) {
                child.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
                modifiedLayouts.add(child.getLayoutParams());
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if(modifiedLayouts.size() > 0) {
             int heightHint = getMeasuredHeight();

            if(heightHint > 0) {
                getLayoutParams().height = heightHint;
            }

            for (ViewGroup.LayoutParams params : modifiedLayouts)
                params.height = LayoutParams.MATCH_PARENT;

            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}