Выровнять вид слева и справа от другого вида, макет ограничения? - PullRequest
1 голос
/ 18 марта 2019

Мне нужно создать представление как изображение ниже с помощью макета ограничения 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>

Согласно приведенному выше коду, виды перекрываются друг с другом.Я хочу, чтобы один вид слева от текстового обзора, а другие были справа.

Ответы [ 4 ]

2 голосов
/ 18 марта 2019

Вы можете использовать цепь для этого Подробнее о проверке цепи: 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>
0 голосов
/ 21 марта 2019
<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" />

0 голосов
/ 18 марта 2019

Если вы немного подкорректируете свой код, вы получите желаемый результат.

<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>
0 голосов
/ 18 марта 2019

Вот код со следующим результатом

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

enter image description here

...