Как добавить представление изображения фиксированной ширины в центре по горизонтали, используя макет ограничения - PullRequest
0 голосов
/ 09 октября 2018

enter image description here

Привет, ребята, мне нужно добавить 2 изображения с фиксированной шириной и высотой в центр экрана, используя схему ограничений, как мы можем достичь чего-то подобного.

xml layout

<?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"
    tools:context=".ui.activity.profile.view.UserDetailsActivity">

    <TextView
        android:id="@+id/tv_label"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="@string/you_are"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent=".1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_percent=".25" />


    <CircularImageView
        android:id="@+id/iv_farmer"
        android:layout_width="@dimen/_100dp"
        android:layout_height="@dimen/_100dp"
        android:src="@drawable/ic_profilewithimage"
        app:civ_border="true"
        app:civ_border_color="#e4e4e4"
        app:civ_border_width="@dimen/_1dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="@+id/iv_prof"
        app:layout_constraintTop_toBottomOf="@+id/tv_label" />

    <CircularImageView
        android:id="@+id/iv_prof"
        android:layout_width="@dimen/_100dp"
        android:layout_height="@dimen/_100dp"
        android:src="@drawable/ic_profilewithimage"
        app:civ_border="true"
        app:civ_border_color="#e4e4e4"
        app:civ_border_width="@dimen/_1dp"
        app:layout_constraintLeft_toRightOf="@+id/iv_farmer"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_label" />

</android.support.constraint.ConstraintLayout>

Я пытаюсь это сделать, но представления не центрированы.

Ответы [ 2 ]

0 голосов
/ 09 октября 2018

Я обновил ваш 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="You are"
        app:layout_constraintBottom_toTopOf="@+id/iv_farmer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent=".1"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_percent=".25" />


    <CircularImageView
        android:id="@+id/iv_farmer"
        android:layout_width="@dimen/_100dp"
        android:layout_height="@dimen/_100dp"
        android:src="@drawable/ic_profilewithimage"
        app:civ_border="true"
        app:civ_border_color="#e4e4e4"
        app:civ_border_width="@dimen/_1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/iv_prof"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CircularImageView
        android:id="@+id/iv_prof"
        android:layout_width="@dimen/_100dp"
        android:layout_height="@dimen/_100dp"
        android:src="@drawable/ic_profilewithimage"
        app:civ_border="true"
        app:civ_border_color="#e4e4e4"
        app:civ_border_width="@dimen/_1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/iv_farmer"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Надеюсь, это поможет:)

0 голосов
/ 09 октября 2018

В вашем первом CircularImageView есть проблема с вашим правым ограничением:

app:layout_constraintRight_toRightOf="@+id/iv_prof"

Это должно быть ограничено слева от iv_prof:

app:layout_constraintRight_toLeftOf="@id/iv_prof"

Если вы хотитечтобы быть ближе друг к другу в центре, затем измените стиль цепочки на packed, добавив этот атрибут к первому CircularImageView:

app:layout_constraintHorizontal_chainStyle="packed"

Для центрирования Views по вертикали в родительском элементе, который вынеобходимо ограничить верхнюю и нижнюю часть каждого View верхним и нижним значениями родительского элемента соответственно.

Также для одних Views вы используете ограничения влево / вправо, а для других - начало / конец.Попробуйте использовать только один их набор по всему макету.

...