You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

启用文本选择时TextView省略失效及布局截断问题求助

Hey there! Let's tackle your two TextView issues one by one:

1. Fixing Ellipsize When Text Selection is Enabled

The problem here is that Android's default TextView ignores the ellipsize attribute when textIsSelectable="true" is set — this is a known behavior because the text selection mode uses a different layout pipeline that prioritizes selectability over text truncation.

Here are two reliable fixes:

Option 1: Enable Selection Programmatically

Instead of setting textIsSelectable="true" in XML, turn on selection in your Activity/Fragment code. This lets the ellipsize setting work as expected:

// Kotlin example
val textView1 = findViewById<TextView>(R.id.textview1)
val textView2 = findViewById<TextView>(R.id.textview2)

textView1.setTextIsSelectable(true)
textView2.setTextIsSelectable(true)

// Force ellipsize to apply after enabling selection
textView1.ellipsize = TextUtils.TruncateAt.END
textView2.ellipsize = TextUtils.TruncateAt.END
// Java example
TextView textView1 = findViewById(R.id.textview1);
TextView textView2 = findViewById(R.id.textview2);

textView1.setTextIsSelectable(true);
textView2.setTextIsSelectable(true);

textView1.setEllipsize(TextUtils.TruncateAt.END);
textView2.setEllipsize(TextUtils.TruncateAt.END);

Don't forget to remove the textIsSelectable="true" line from your XML when using this method.

Option 2: Custom TextView for Reusability

If you need a solution you can reuse across your app, create a custom TextView that overrides layout logic to respect both selection and ellipsize:

class SelectableEllipsizedTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = android.R.attr.textViewStyle
) : AppCompatTextView(context, attrs, defStyleAttr) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (textIsSelectable && ellipsize != null) {
            val layout = layout ?: return
            if (layout.lineCount > maxLines) {
                val truncatedText = TextUtils.ellipsize(
                    text, paint, measuredWidth.toFloat(),
                    ellipsize, true, null
                )
                text = truncatedText
            }
        }
    }
}

Then replace the default TextView in your XML with this custom view:

<com.yourpackage.SelectableEllipsizedTextView
    android:id="@+id/textview1"
    <!-- keep all your existing attributes -->
    android:textIsSelectable="true" />

2. Fixing First Line Truncation in textview2

The first line truncation issue after adding paddingRight to textview1 is likely due to a conflict between textIsSelectable="true", maxLines="2", and layout gravity settings. Try these fixes:

Solution 1: Add scrollHorizontally="false"

Modify textview2's XML to force proper text wrapping instead of horizontal scrolling, which can cause unexpected truncation in selectable multi-line text:

<TextView
    android:id="@+id/textview2"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:ellipsize="end"
    android:includeFontPadding="false"
    android:maxLines="2"
    android:scrollHorizontally="false" <!-- Add this line -->
    android:text="MultiLine123456789123456789123456789123456789 123456789123456789123456789"
    android:textColor="#000"
    android:textIsSelectable="true"
    android:textSize="16dp" />

Solution 2: Adjust Font Padding or Line Spacing

The includeFontPadding="false" attribute can sometimes disrupt line height calculations for selectable multi-line text. Try removing it, or add line spacing to compensate:

android:includeFontPadding="false"
android:lineSpacingExtra="2dp" <!-- Add this to fix line height alignment -->

Solution 3: Force Layout Refresh in Code

If XML adjustments don't work, trigger a layout refresh for textview2 to fix the truncation:

textView2.post {
    textView2.requestLayout()
    textView2.invalidate()
}

内容的提问来源于stack exchange,提问作者sathish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 16:57:44