ConstraintLayout中心对齐图像和文本

kos*_*sas 3 android android-layout android-constraintlayout

我该如何调整TextView结束的ImageViewTextView会是centered verticallyConstraintLayout我设法将其调整到年底ImageView,但它不是垂直居中,因为ImageView是大一点比TextView我知道如何用它来办RelativeLayout,但想使用的最佳实践,并且只使用ConstraintLayout

以下是它应该如何显示的示例(云图标和文本上次备份)

在此输入图像描述

Hon*_*uan 6

只要使用app:layout_constraintTop_toTopOfapp:layout_constraintBottom_toBottomOf在一起,就会造成TextView中心垂直对齐的ImageView.

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:text="Last Backup"
    app:layout_constraintStart_toEndOf="@+id/imageView"
    app:layout_constraintTop_toTopOf="@+id/imageView"
    app:layout_constraintBottom_toBottomOf="@+id/imageView"
    />
Run Code Online (Sandbox Code Playgroud)


San*_*ish 5

drawable像这样使用left 作为你的 TextView 并根据需要按gravity你的 root进行更改layout

<TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:drawablePadding="15dp"
     android:drawableLeft="@drawable/google"
     android:text="@string/textGoogle" />
Run Code Online (Sandbox Code Playgroud)