繁体   English   中英

当孩子的高度是 wrap_content 时,约束布局正在占用全宽

[英]Constraint Layout is taking full width when a child is wrap_content in height

我希望测试视图的高度与卡的 rest 占用的高度相同。 (请参阅代码片段末尾的图像)但它将视图的高度推到整个屏幕。

只有在提供 0 高度后它才能按我们想要的方式工作,这似乎不正确。 除了提供0 高度来实现这种行为之外,还能做什么?

<RelativeLayout 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_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary">


        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="60dp"
            android:layout_marginEnd="8dp"
            android:background="@android:color/holo_red_light"

            android:text="Dialog title"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="HardcodedText" />


        <View
            android:id="@+id/test"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_width="5dp"
            android:layout_height="wrap_content"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>```


 what I want   [1]: https://i.stack.imgur.com/8NU4d.png
 what shows up [2]: https://i.stack.imgur.com/K6qEn.png

试试这个,您需要更改View的底部约束,并使View的高度为 0dp。

<RelativeLayout 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_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/echo">


        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="60dp"
            android:layout_marginEnd="8dp"
            android:background="@android:color/holo_red_light"

            android:text="Dialog title"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="HardcodedText" />


        <View
            android:id="@+id/test"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@color/black"
            app:layout_constraintBottom_toBottomOf="@+id/dialog_title"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_width="5dp"
            android:layout_height="0dp"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>

Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM