如何在Android中将图标垂直对齐到TextView第一行的中心

7
我想把左边的图标(请参考下面的图片)垂直居中到TextView的第一行文本。我找不到方法,尝试了在TextView上使用drawableLeft、gravity和constraints,但都没有帮助。有人能帮帮我吗?
 <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp">

    <ImageView
        android:id="@+id/iv_itm_tick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_tick_mark"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        app:layout_constraintStart_toEndOf="@+id/iv_itm_tick"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Concepts of deep learning and machine learning workflow" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here


只需将图像的约束限制在文本视图的顶部和底部即可。这意味着将图像视图的顶部约束限制为文本视图的顶部,底部约束限制为文本视图的底部。 - Saurav Kumar
1
在这种情况下,它将是整个段落的垂直居中,而不是第一行。 - Ajay J G
2个回答

8
如果ImageViewTextView,你可以简单地将其基线约束到另一个TextView的基线上。不幸的是,ImageViews没有可行的基线概念,因此不能简单地将ImageView的基线约束到TextView上。(尝试一下会发生什么。)
解决方法之一是用TextView替换ImageView,将图标设置为TextView的背景,并约束两个基线。如果没有特殊考虑,这种技术会导致选中标记的缩放并且会导致失真。为了解决这个问题,将选中标记drawable放置在一个layer-list drawable中,并指定重力。正是重力防止了缩放: tick_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ic_tick_mark"
        android:gravity="center" />
</layer-list>

选项卡标记可以看起来像这样:

ic_tick_mark.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:tint="#66A8DD"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="#FFFFFFFF"
        android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z" />
</vector>

如果底层图标是位图,则图层列表也可以是XML位图可绘制对象。

另一种方法是创建一个空的TextView,它具有要附加勾号的TextView的相同特性。将空的TextView约束到其他TextView的基线上。这将使空的文本视图与其他TextView的第一行并排。

现在,将ImageView取出,并将其顶部和底部约束到空的TextView上。勾号将按照所需的方式对齐。

这是一种仅使用XML的解决方案。其他解决方案可能涉及一些编程。

enter image description here

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv_itm_tick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_tick_mark"
        app:layout_constraintBottom_toBottomOf="@id/dummyView"
        app:layout_constraintEnd_toStartOf="@id/textView"
        app:layout_constraintTop_toTopOf="@id/dummyView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Concepts of deep learning and machine learning workflow"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/dummyView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintBaseline_toBaselineOf="@id/textView"
        app:layout_constraintEnd_toStartOf="@+id/textView"/>

</androidx.constraintlayout.widget.ConstraintLayout>

-1

按照以下方式编辑您的XML:

<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">

<ImageView
    android:id="@+id/iv_itm_tick"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/ic_tick_mark"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:text="Concepts of deep learning and machine learning workflow"
    app:layout_constraintStart_toEndOf="@+id/iv_itm_tick"
    android:layout_marginTop="10dp"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接