简体   繁体   中英

How to remove top and bottom padding on a textView?

I have a textView which I want to remove every padding and extra space at the top and bottom of the text. This is my XML:

            <TextView
                android:id="@+id/stid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/quicksand"
                android:gravity="center_horizontal"
                android:includeFontPadding="false"
                android:maxLines="1"
                android:text="@string/st"
                android:textSize="40sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.538"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

I am wrapping content and removing font padding but there is still a large padding at the top and at the bottom of the text which I want to remove. (on the sides it is fine just top and bottom)

this is a screenshot as requested

在此处输入图片说明

Did you try?

android:includeFontPadding="false"
android:lineSpacingExtra="-5dp"

Use this class and compile your project.

public class NoPaddingTextView extends TextView {

private int mAdditionalPadding;

public NoPaddingTextView(Context context) {
    super(context);
    init();
}


public NoPaddingTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    setIncludeFontPadding(false);
}

@Override
protected void onDraw(Canvas canvas) {
    int yOff = -mAdditionalPadding / 6;
    canvas.translate(0, yOff);
    super.onDraw(canvas);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    getAdditionalPadding();

    int mode = MeasureSpec.getMode(heightMeasureSpec);
    if (mode != MeasureSpec.EXACTLY) {
        int measureHeight = measureHeight(getText().toString(), widthMeasureSpec);

        int height = measureHeight - mAdditionalPadding;
        height += getPaddingTop() + getPaddingBottom();
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

private int measureHeight(String text, int widthMeasureSpec) {
    float textSize = getTextSize();

    TextView textView = new TextView(getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    textView.setText(text);
    textView.measure(widthMeasureSpec, 0);
    return textView.getMeasuredHeight();
}

private int getAdditionalPadding() {
    float textSize = getTextSize();

    TextView textView = new TextView(getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    textView.setLines(1);
    textView.measure(0, 0);
    int measuredHeight = textView.getMeasuredHeight();
    if (measuredHeight - textSize > 0) {
        mAdditionalPadding = (int) (measuredHeight - textSize);
        Log.v("NoPaddingTextView", "onMeasure: height=" + measuredHeight + " textSize=" + textSize + " mAdditionalPadding=" + mAdditionalPadding);
    }
    return mAdditionalPadding;
}

}

Replace TextView with NoPaddingTextView Class Name in your XML file

<YourPackage.NoPaddingTextView
            android:id="@+id/stid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/quicksand"
            android:gravity="center_horizontal"
            android:includeFontPadding="false"
            android:maxLines="1"
            android:text="@string/st"
            android:textSize="40sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.538"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

图片

Source : Github StackOverFlow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM