gpt4 book ai didi

安卓约束布局 : Views not respecting margins

转载 作者:行者123 更新时间:2023-11-29 16:30:10 28 4
gpt4 key购买 nike

我正在尝试创建一个布局,其中各种数据由一条线( View )分隔,但该设计要求与该线的顶部和底部间距相等。我正在尝试使用边距来完成此操作,但没有得到预期的结果。基于official documentation ,只要为边缘方向设置了约束,就应该遵守它们。出于某种原因,上边距给了我们正确的间距,但是下边距根本没有给出任何间距。我创建了一个示例,其中仅包含要重现的相关布局 axml,以及足够大的边距以可视化问题:

<?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:padding="20dp">
<TextView android:text="NAME"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/account_name_label"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView android:text="Your Name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/account_name"
app:layout_constraintTop_toBottomOf="@+id/account_name_label"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<View
android:id="@+id/line1"
app:layout_constraintTop_toBottomOf="@+id/account_name"
app:layout_constraintBottom_toTopOf="@+id/account_joined_label"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#000000"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
/>
<TextView android:text="JOINED"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/account_joined_label"
app:layout_constraintTop_toBottomOf="@+id/line1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<TextView android:text="January 1, 2019"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/account_joined"
app:layout_constraintTop_toBottomOf="@+id/account_joined_label"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</android.support.constraint.ConstraintLayout>

以上结果在视觉上如下: ConstraintLayout margin issue

现在,我可以将 android:layout_marginTop="100dp" 添加到行 View 之后的第一个 TextView 以“修复”这个问题,但是我想重用行 View 的样式,而无需不必担心记住要为行 View 之后的第一个 View 添加上边距。我错过了什么?

更新:布局应该如下所示:

ConstraintLayout corrected layout

最佳答案

您需要将 account_joined 的底部连接到 parent 的底部,将 account_joined_label 的底部连接到 account_joined 的顶部。所以你可以试试这个:

<?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="match_parent"
android:padding="20dp">

<TextView android:text="NAME"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/account_name_label"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<TextView android:text="Your Name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/account_name"
app:layout_constraintTop_toBottomOf="@+id/account_name_label"
app:layout_constraintRight_toRightOf="parent" />

<View
android:id="@+id/line1"
app:layout_constraintTop_toBottomOf="@+id/account_name"
app:layout_constraintBottom_toTopOf="@+id/account_joined_label"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#000000"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"/>

<TextView android:text="JOINED"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/account_joined_label"
app:layout_constraintTop_toBottomOf="@+id/line1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/account_joined" />

<TextView android:text="January 1, 2019"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/account_joined"
app:layout_constraintTop_toBottomOf="@+id/account_joined_label"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

关于安卓约束布局 : Views not respecting margins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56810032/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com