Remove space between stacked TextViews

后端 未结 8 1785
南旧
南旧 2020-12-09 02:27

I have a vertical LinearLayout with two TextView inside it. The former contains a static text property (it\'s text never change) and the last conta

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 03:15

    If you set includeFontPadding to false it helps.

    android:includeFontPadding="false"
    

    but if you know you don't have any descenders because you set

    android:textAllCaps="true"
    

    or you know there are no characters which have descender, you can make a custom TextView and set the height to the baseline.

    public class ExTextView extends TextView {
    
        public ExTextView(Context context) {
            this(context, null);
        }
    
        public ExTextView(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public ExTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public ExTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            setHeight(getBaseline());  // <--- Shrink size to baseline
            super.onDraw(canvas);
        }
    }
    

    The source code is included in my UiComponents test app. https://github.com/landenlabs/UiComponents

提交回复
热议问题