这是我的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>
按照上面的代码,视图彼此重叠。我希望一个视图位于textview的左侧,而其他视图位于右侧。
答案 0 :(得分:2)
您可以使用链条 有关链式检查的更多详细信息: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>
答案 1 :(得分:0)
这是带有以下结果的代码
<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="wrap_content"
android:layout_height="5dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="10dp"
android:background="@android:color/holo_orange_dark"
app:layout_constraintRight_toLeftOf="@+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="wrap_content"
android:layout_height="5dp"
android:layout_marginStart="10dp"
android:layout_marginTop="120dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintLeft_toRightOf="@+id/or"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 2 :(得分:0)
如果您像这样稍微调整代码,将获得所需的结果。
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/left"
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"
android:layout_height="5dp"
android:layout_marginStart="20dp"
android:layout_marginTop="120dp"
android:background="@android:color/holo_orange_dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@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_toRightOf="@id/left"
app:layout_constraintRight_toLeftOf="@id/right"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/right"
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"
android:layout_height="5dp"
android:layout_marginTop="120dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintLeft_toRightOf="@id/or"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 3 :(得分:0)
<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="@android:color/white">
<View
android:id="@+id/left"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_margin="20dp"
android:background="@android:color/black"
app:layout_constraintEnd_toStartOf="@+id/or"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OR"
android:textSize="20sp"
android:layout_marginTop="6dp"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/right"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_margin="20dp"
android:background="@android:color/black"
app:layout_constraintStart_toEndOf="@+id/or"
app:layout_constraintTop_toTopOf="parent" />