我有一个可以跨越两行的居中文本的 TextView。有时会出现一个 CompoundDrawable 并与第一行文本对齐。在这种情况下,我希望第二行文本与整个顶行对齐(即可绘制+文本)。它当前仅与文本对齐。
这是文本视图 xml:
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/margin_small"
android:layout_marginEnd="@dimen/margin_medium"
android:layout_marginTop="@dimen/margin_small"
android:layout_marginStart="@dimen/margin_medium"
android:gravity="center"
android:maxLines="2"
app:layout_constraintBottom_toTopOf="@+id/item_subtitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/item_icon"
app:layout_goneMarginStart="@dimen/margin_medium"
tools:text="Headline longer than than the one line name and some more info Headline longer than than the one line name and some more inf"/>
这是针对compoundDrawable(我使用此解决方案将drawable与第一行对齐):
val innerDrawable =itemView.resources.getDrawable(R.drawable.ic_item_indicator_premium)
val premiumCompoundDrawable = PremiumCompoundDrawable(innerDrawable)
innerDrawable.setBounds(0,0,innerDrawable.intrinsicWidth, innerDrawable.intrinsicHeight)
premiumCompoundDrawable.setBounds(0, 0, innerDrawable.intrinsicWidth, innerDrawable.intrinsicHeight)
titleTextView.setCompoundDrawables(premiumCompoundDrawable, null, null, null)