【问题标题】:How to remove top and bottom padding on a textView?如何删除 textView 上的顶部和底部填充?
【发布时间】:2019-12-29 08:46:36
【问题描述】:

我有一个 textView,我想删除文本顶部和底部的所有填充和额外空间。这是我的 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" />

我正在包装内容并删除字体填充,但在我要删除的文本的顶部和底部仍然有一个大的填充。 (在侧面,顶部和底部都很好)

这是应要求的屏幕截图

【问题讨论】:

  • 分享截图和你的完整xml文件
  • @YashKrishan 添加了截图。 XML 太大,无法在此处显示。但是这个文本视图被包裹在一个带有另一个 textView(对象)的约束视图中。所有这些都在另一个约束布局中
  • 负填充什么都不做。
  • 如果对您有帮助,请验证答案。

标签: android textview padding android-wrap-content


【解决方案1】:

你试过了吗?

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

【讨论】:

    【解决方案2】:

    使用这个类并编译你的项目。

    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;
    }
    

    }

    在您的 XML 文件中将 TextView 替换为 NoPaddingTextView 类名

    <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" />
    

    来源:Github StackOverFlow

    【讨论】:

    • 如果您要通过 xml 属性设置子类,则该子类已过时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多