在ConstraintLayout中从其中心位置垂直偏移图像

时间:2019-10-16 08:05:24

标签: android-constraintlayout

我将图像在屏幕上居中放置,如下所示:

<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不起作用。所要做的就是在屏幕顶部限制图像的位置添加空白。图像仍保留在中央。但我希望它向下移动50dp。

1 个答案:

答案 0 :(得分:0)

  

我尝试了一下,它工作正常,并且突出显示了我更改的代码



     <?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"
             android:src="@drawable/two"
             android:layout_marginTop="50dp"
             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"
             android:src="@drawable/ic_launcher_background"
             app:layout_constraintVertical_bias="0.60"  //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