Смещение изображения по вертикали в ConstraintLayout от его центральной позиции - PullRequest
1 голос
/ 16 октября 2019

Я центрирую изображение на экране следующим образом:

<androidx.constraintlayout.motion.widget.ConstraintLayout
    android:id="@+id/dashboardConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/logo"
        android:layout_width="155dp"
        android:layout_height="20dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/ic_logo" 
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.motion.widget.ConstraintLayout>

То, что я хотел бы сделать, это сместить изображение вниз на 50dp от его центрального положения. Добавление marginTop не работает. Все, что делает, это добавляет поле в верхней части экрана, где изображение ограничено. Изображение по-прежнему остается в центре. Но я хочу, чтобы он сдвигался вниз на 50 дп.

1 Ответ

0 голосов
/ 16 октября 2019

Я попробовал это, и он отлично работает, и я выделил код, который я изменил



     <?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"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".Third">

         <androidx.appcompat.widget.AppCompatImageView //it is normar imageView in centered 
             android:id="@+id/logo"
             android:layout_width="155dp"
             android:layout_height="20dp"
             android:scaleType="centerCrop"
             android:src="@drawable/ic_launcher_background"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent"/>

         <androidx.appcompat.widget.AppCompatImageView //in this imageView i used marginTop and it works perfectly
             android:id="@+id/logo1"
             android:layout_width="155dp"
             android:layout_height="20dp"
             android:scaleType="centerCrop"
             <b>android:src="@drawable/two"</b>
             <b>android:layout_marginTop="50dp"</b>
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent"/>

         <androidx.appcompat.widget.AppCompatImageView //in this imageView i used vertical_bias and it work perfectly.
             android:id="@+id/logo2"
             android:layout_width="155dp"
             android:layout_height="20dp"
             android:scaleType="centerCrop"
             <b>android:src="@drawable/ic_launcher_background"
             app:layout_constraintVertical_bias="0.60"</b>  //there is 0.0=Top,  0.50=center,  1.0=Bottom and so on.
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent"/>
     </androidx.constraintlayout.widget.ConstraintLayout>

Вывод: -

enter image description here

...