Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ConstraintLayout:将行中所有视图的高度设置为与最高视图匹配_Android_Android Constraintlayout - Fatal编程技术网

Android ConstraintLayout:将行中所有视图的高度设置为与最高视图匹配

Android ConstraintLayout:将行中所有视图的高度设置为与最高视图匹配,android,android-constraintlayout,Android,Android Constraintlayout,我试图利用ConstraintLayout(版本1.0.2)设置两个并排视图的高度,以匹配其中最高的一个。这在RecyclerView中充当ViewHolder,其中每个TextView获得任意长度的文本 如果我将每个设置为“包装内容”,则较短的内容将收缩。 如果我将两者都设置为0dp(匹配约束),则两者的高度均为0 以下是设置: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.Constra

我试图利用ConstraintLayout(版本1.0.2)设置两个并排视图的高度,以匹配其中最高的一个。这在RecyclerView中充当ViewHolder,其中每个TextView获得任意长度的文本

如果我将每个设置为“包装内容”,则较短的内容将收缩。 如果我将两者都设置为0dp(匹配约束),则两者的高度均为0

以下是设置:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/id1"
        android:layout_width="60dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/id2"/>

    <TextView
        android:id="@+id/id2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/id1"
        app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

我想这是一个bug,因为“0dp”应该更像match_parent,而不是实际的0dp

顺便说一下,在iOS上,等效的自动布局规则(设置视图高度以匹配父视图的顶部和底部)会产生预期的结果

当然,使用LinearLayout很简单,但是这个布局在更大的布局中起作用,我想修剪视图层次…

这可能会对您有所帮助

<android.support.constraint.ConstraintLayout
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:padding="@dimen/activity_vertical_margin"
        android:text="sjdjhshdjhdjhsdgfjhgsdjfgjsdgfjsdgfhgdsjhfghs"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tv_2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:padding="@dimen/activity_vertical_margin"
        android:text="sjdjhsjhd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tv_1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


回答,以防将来有人在寻找答案

v1.1中引入,以实现OP在问题中提出的一种功能

可以使用以下xml实现上述功能:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  <TextView
      android:id="@+id/id1"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintBottom_toBottomOf="@+id/barrier"
      app:layout_constraintVertical_bias="0.0"
      android:text="@string/id1_text" />

  <TextView
      android:id="@+id/id2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_toRightOf="@+id/id1"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintBottom_toBottomOf="@+id/barrier"
      app:layout_constraintVertical_bias="0.0"
      android:text="@string/id2_text" />

  <android.support.constraint.Barrier
      android:id="@+id/barrier"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:barrierDirection="bottom"
      app:constraint_referenced_ids="id1,id2" />

</android.support.constraint.ConstraintLayout>



这是一个复杂而缓慢的过程,但它是有效的

重点是:

  • 为“测量高度”添加其他不可见的相同布局。这一个有
    wrap\u content
    height属性,所以如果它足够大,它将拉伸父对象
  • 将可见布局高度设置为0dp,以便填充父级的其余部分
正如您在下面看到的捕获,我们有两种布局。隐藏的一个是包装内容



不起作用。也许您的垂直填充隐藏了问题。无论如何,不支持match_parent,并将其视为0dp,AFAIK。我注意到,对于固定宽度(60dp)和match_parent height,它将起作用。如果这起作用,我想这只是因为两个高度匹配的孩子是他们父母中唯一的两个。如果您有一个复杂的布局(如果您使用“ConstraintLayout”,您什么时候不会这样做),这将不起作用。您应该使用Ctrl+K或Cmd+K来正确格式化源代码块。实际上,我认为这根本不起作用。通过在
id1
上放置
0dp
layout\u height
s,并在
id2
上放置
wrap\u content
,您的意思是
id2
必须适合其内容,
id1
必须匹配任何内容。也就是说,如果
id1
高于
id2
,该行将不会调整大小以适应它。@Droidwala但是
layou height
应该设置为什么?如果
0dp
则它会占用未定义的空间量,因为没有定义屏障对接的高度。如果
wrap_content
元素只取其内容的高度,而不管屏障在哪里。所以看起来屏障不能用来改变它们所指事物的高度。。。正确的?还是我遗漏了什么?@Droidwala,谢谢。这是原始问题中的用例——我得出的结论是屏障不能做到这一点,您已经证实了这一点。令人沮丧,但感谢您的评论:)所以这不是答案,这是不可能的?可以使用LinearLayout完成,将2视图与高度匹配\u父视图放在其中,而父视图的高度是包装内容,以帮助使用屏障。我不明白。为什么这能解决问题?据我们所知,您的父站点可以有一个固定的大小。编辑:我找到了您的站点,可以看到完整的源代码。看起来您只是将其他视图与第一个视图的高度相匹配。这仅在第一个视图比其他视图大时有效,而在第二个/第三个视图增大时无效
<TextView
    android:id="@+id/txt_service_request_id"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_rectangle_blue"
    android:gravity="center"
    android:padding="8dp"
    android:text="@string/service_request_id"
    android:textColor="@android:color/white"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/txt_service_request_status"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/txt_service_request_status"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/bg_rectangle_blue"
    android:gravity="center"
    android:padding="8dp"
    android:text="@string/service_request_status"
    android:textColor="@android:color/white"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="@+id/txt_service_request_id"
    app:layout_constraintEnd_toStartOf="@+id/txt_service_request_desc"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/txt_service_request_id"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:id="@+id/txt_service_request_desc"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/bg_rectangle_blue"
    android:gravity="center"
    android:padding="8dp"
    android:text="@string/service_request_desc"
    android:textColor="@android:color/white"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="@+id/txt_service_request_id"
    app:layout_constraintEnd_toStartOf="@+id/txt_service_request_cat"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/txt_service_request_status"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/txt_service_request_cat"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/bg_rectangle_blue"
    android:gravity="center"
    android:padding="8dp"
    android:text="@string/service_request_cat"
    android:textColor="@android:color/white"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="@+id/txt_service_request_id"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/txt_service_request_desc"
    app:layout_constraintTop_toTopOf="parent" />
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">

    <TextView
        android:id="@+id/view_1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="#DDDDDD"
        android:text="@android:string/yes"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/view_2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/view_1_invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#DDDDDD"
        android:text="@android:string/yes"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/view_2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="#DDDDDD"
        android:text="@android:string/httpErrorBadUrl"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/view_1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/view_2_invisible"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="#DDDDDD"
        android:text="@android:string/httpErrorBadUrl"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/view_1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>