Android: Last line of textview cut off

后端 未结 20 1143
北荒
北荒 2020-11-28 02:46

I have a horizontal LinearLayout containing a TextView followed by a Spinner next to it. This LinearLayout is dynamically

相关标签:
20条回答
  • 2020-11-28 03:28

    Best workaround for this is to add a dummy View of desired height (i.e. this will add padding itself) at the bottom of your view.

    <TableRow
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="5dp"
     android:layout_marginRight="5dp" >
         <View 
           android:layout_width="match_parent" 
           android:layout_height="30dp"/>
    </TableRow>
    

    Like in my case I added one more table row at the bottom of the view. Hope this could help someone.

    0 讨论(0)
  • 2020-11-28 03:30

    For me, this solution worked like a charm.

    The height and width of my outermost layout was set dynamically, so the TextView contained within got it's text cut even if I set android:maxLines in my xml (for different devices it was behaving differently).

    After trying out different methods, finally I got a solution that fixed my issue.

    Textview:

    public class CustomTextView extends androidx.appcompat.widget.AppCompatTextView {
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public CustomTextView(Context context) {
            super(context);
        }
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            // set fitting lines to prevent cut text
            int fittingLines = h / this.getLineHeight();
            if (fittingLines > 0) {
                this.setLines(fittingLines);
                this.setEllipsize(TextUtils.TruncateAt.END);
            }
        }
    }
    

    xml:

    <com.myproject.android.customviews.CustomTextView
        android:id="@+id/tv_partner_description"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top"
        android:textColor="@color/white"
        android:textSize="@dimen/text_small_medium" />
    
    0 讨论(0)
提交回复
热议问题