gpt4 book ai didi

android - 将 View 与另一个 View 的左右位置对齐,约束布局?

转载 作者:行者123 更新时间:2023-11-29 18:33:20 25 4
gpt4 key购买 nike

我需要借助约束布局创建如下图所示的 View enter image description here

这是我的 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_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">

<View
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="120dp"
android:background="@android:color/holo_orange_dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintLeft_toRightOf="@+id/or"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="OR"
android:textSize="40sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<View
android:id="@+id/right"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginTop="120dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/or"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

根据上面的代码, View 相互重叠。我希望一个 View 位于 textview 的左侧,而其他 View 位于右侧。

最佳答案

You can use chain for that For more detail about chain check: https://constraintlayout.com/basics/create_chains.html

<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">


<View
android:id="@+id/left"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_marginTop="125dp"
android:layout_marginEnd="@dimen/_5sdp"
android:layout_marginStart="@dimen/_5sdp"
app:layout_constraintHorizontal_weight="1"
android:background="@android:color/holo_orange_dark"
app:layout_constraintEnd_toStartOf="@+id/or"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="OR"
android:textSize="40sp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintEnd_toStartOf="@+id/right"
app:layout_constraintStart_toEndOf="@+id/left"
app:layout_constraintTop_toTopOf="parent" />

<View
android:id="@+id/right"
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_marginStart="@dimen/_5sdp"
android:layout_marginEnd="@dimen/_5sdp"
app:layout_constraintHorizontal_weight="1"
android:background="@android:color/holo_red_dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/or"
app:layout_constraintTop_toTopOf="@+id/left" />

</androidx.constraintlayout.widget.ConstraintLayout>

关于android - 将 View 与另一个 View 的左右位置对齐,约束布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55221630/

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