我想将左侧图标(请参阅下图)垂直居中放置在 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>
更新:对于任何偶然发现这个答案的人:不确定我在想什么,但我相信这个是正确的答案。在该答案中,使用了
android:baselineAlignBottom="true"
,虽然没有很好的记录,但似乎是秘密武器。
如果 ImageView 是 TextView,您可以简单地将其基线限制为另一个 TextView 的基线。不幸的是,ImageViews实际上没有可行的基线概念,因此您不能简单地将ImageView的基线约束到TextView。 (尝试看看会发生什么。)
一种方法是将ImageView替换为TextView,将图标设置为TextView的背景并约束两条基线。如果没有特殊考虑,这种技术将导致刻度线缩放并导致失真。要解决此问题,请将刻度线可绘制对象放置在图层列表可绘制对象中并指定重力。正是重力阻止了结垢:
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 解决方案。其他解决方案可能涉及一些编码。
<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>
像这样编辑你的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>