启用文本选择时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

