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

Aja*_*J G 6 android textview imageview android-layout android-constraintlayout

我想放置我的左图标(请参考下图),垂直居中到 TextView 中文本的第一行。我找不到方法,我在文本视图、重力和约束上尝试了drawableLeft,但这没有帮助。有谁能够帮助我?

 <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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Che*_*amp 9

更新: 对于任何遇到这个答案的人:不确定我在想什么,但我相信是正确的答案。在该答案中,android:baselineAlignBottom="true"使用的实际上是ImageView 的属性

旧答案:
如果ImageViewTextView您可以简单地将其基线限制为另一个TextView的基线。不幸的是,ImageView并没有真正可行的基线概念,因此您不能简单地将ImageView的基线约束到TextView。(尝试看看会发生什么。) 请参阅上面的更新。

一种方法是将ImageView替换为TextView,将图标设置为TextView 的背景并约束两条基线。如果没有特殊考虑,这种技术将导致刻度线缩放并导致失真。要解决此问题,请将刻度线可绘制对象放置在图层列表可绘制对象中并指定重力。正是重力阻止了结垢:

勾选背景.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ic_tick_mark"
        android:gravity="center" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)

刻度线可能如下所示:

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>
Run Code Online (Sandbox Code Playgroud)

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

另一种可行的方法是创建一个空的TextView ,它与要附加刻度线的TextView具有相同的特征。将空TextView约束到另一个TextView的基线。这会将空 textview 放置在另一个TextView的第一行旁边。

现在,获取ImageView并将其顶部和底部限制为空TextView。刻度线将按需要排列。

这是一个纯 XML 解决方案。其他解决方案可能涉及一些编码。

在此输入图像描述

<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>
Run Code Online (Sandbox Code Playgroud)


saj*_*jad -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>
Run Code Online (Sandbox Code Playgroud)