How to remove the top and bottom space on textview of Android

前端 未结 14 1759
执笔经年
执笔经年 2020-11-28 04:58

When I include the below XML to layout file, I can see the below image. If you see it, you could realize that the TextView has top and bot

相关标签:
14条回答
  • 2020-11-28 05:16

    android:includeFontPadding="false" is pretty good but it does not get it precisely. sometimes you want border line accuracy so you can figure it out yourself by applying negative margins:

    try setting your bottom and top margins to a negative value.

    something like this:

    android:layout_marginTop="-5dp"
    android:layout_marginBottom="-5dp"
    

    adjust the values accordingly.

    0 讨论(0)
  • 2020-11-28 05:19

    This is the code that saved our day. It was adapted using mono C# code from maksimko:

    public class TopAlignedTextView extends TextView {
    
        public TopAlignedTextView(Context context) {
            super(context);
        }
    
        /*This is where the magic happens*/
        @Override
        protected void onDraw(Canvas canvas){
    
            float offset = getTextSize() - getLineHeight();
            canvas.translate(0, offset);
            super.onDraw(canvas);
        }
    }
    

    Still had to play around with textView.setIncludeFontPadding(false) because we were aligning TextViews with different font sizes.

    0 讨论(0)
  • 2020-11-28 05:23

    I had the same problem. Attribute android:includeFontPadding="false" does not work for me. I've solved this problem in this way:

    public class TextViewWithoutPaddings extends TextView {
    
        private final Paint mPaint = new Paint();
    
        private final Rect mBounds = new Rect();
    
        public TextViewWithoutPaddings(Context context) {
            super(context);
        }
    
        public TextViewWithoutPaddings(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public TextViewWithoutPaddings(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onDraw(@NonNull Canvas canvas) {
            final String text = calculateTextParams();
    
            final int left = mBounds.left;
            final int bottom = mBounds.bottom;
            mBounds.offset(-mBounds.left, -mBounds.top);
            mPaint.setAntiAlias(true);
            mPaint.setColor(getCurrentTextColor());
            canvas.drawText(text, -left, mBounds.bottom - bottom, mPaint);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            calculateTextParams();
            setMeasuredDimension(mBounds.width() + 1, -mBounds.top + 1);
        }
    
        private String calculateTextParams() {
            final String text = getText().toString();
            final int textLength = text.length();
            mPaint.setTextSize(getTextSize());
            mPaint.getTextBounds(text, 0, textLength, mBounds);
            if (textLength == 0) {
                mBounds.right = mBounds.left;
            }
            return text;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:23

    android:includeFontPadding="false"

    0 讨论(0)
  • 2020-11-28 05:26

    Try android:includeFontPadding="false" to see if it helps. In my experience that will help a little bit, but there's no way of reducing the TextView dimensions to the exact pixel-perfect text size.

    The only alternative, which may or may not give better results, is to cheat a bit and hard-wire the dimensions to match the text size, e.g. "24sp" instead of "wrap_content" for the height.

    0 讨论(0)
  • 2020-11-28 05:29

    Have you defined a layout margin? For example:

    android:layout_marginTop="5dp"
    

    Otherwise, if your text view is wrapped inside a LinearLayout or other container, then that cold have either padding or a margin too.

    0 讨论(0)
提交回复
热议问题