【问题标题】:Align second line in textView to center, with compoundDrawable aligned to first将 textView 中的第二行对齐到中心,compoundDrawable 对齐到第一行
【发布时间】:2019-09-02 10:45:31
【问题描述】:

我有一个可以跨越两行的居中文本的 TextView。有时会出现一个 CompoundDrawable 并与第一行文本对齐。在这种情况下,我希望第二行文本与整个顶行对齐(即 drawable+text)。它目前仅与文本对齐。

I'd like it to look like this

Here's what I'm getting

这是 TextView 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(我使用this 解决方案将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)

【问题讨论】:

标签: android kotlin textview android-constraintlayout compound-drawables


【解决方案1】:

您可以使用 Spannable String 来满足您的要求,如下所示:

XML 文件:

<ImageView
        android:id="@+id/item_icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/table_bg"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="20dp"/>
<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="5dp"
        android:layout_marginEnd="2dp"
        android:layout_marginTop="5dp"
        android:layout_marginStart="2dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/item_icon"
        app:layout_goneMarginStart="8dp"
        android:maxLines="2"
        android: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"/>

在 Java 中

        itemTitle=findViewById(R.id.item_title);
        ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_star); //Find your drawable.
        SpannableString spannableString = new SpannableString(itemTitle.getText()); //Set text of SpannableString from TextView
        spannableString.setSpan(imageSpan, 0, itemTitle.getText().length()+1-itemTitle.getText().length(), 0); //Add image at end of string
        itemTitle.setText(spannableString);

在 Kotlin 中:

val imageSpan = ImageSpan(this, R.drawable.ic_star) //Find your drawable.
        val spannableString = SpannableString(item_title.text) //Set text of SpannableString from TextView
        spannableString.setSpan(
            imageSpan,
            0,
            item_title.text.length + 1 - item_title.text.length,
            0
        ) //Add image at start of string
        item_title.setText(spannableString)

输出是:

我希望它对你有用。

【讨论】:

    猜你喜欢
    • 2014-06-06
    • 2020-11-23
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    相关资源
    最近更新 更多