当我有一个TextView与一个\n在文本中,,在右边我有两个单线TextViews,一个在另一个之间没有间距。我已经为所有三个textview设置了以下内容。

android:lineSpacingMultiplier="1" 
android:lineSpacingExtra="0pt" 
android:paddingTop="0pt" 
android:paddingBottom="0pt"

第一行的左侧TextView线完美地与右上角TextView。

左TextView的第二行略高于右下TextView的第二行。

似乎有某种隐藏的填充顶部和底部的TextViews。我怎么才能去掉它呢?


当前回答

我能感受到你的痛苦。我已经尝试了上面的每个答案,包括setIncludeFontPadding为false,这对我没有任何帮助。

我的解决方案吗?layout_marginBottom="-3dp"在TextView给你一个解决方案的底部, 砰!

虽然,-3dp在layout_marginTop失败....ugh。

其他回答

在imageviewxml中使用这个

android: adjustViewBounds = " true "

我删除间距在我的自定义视图- NoPaddingTextView。

https://github.com/SenhLinsh/NoPaddingTextView

package com.linsh.nopaddingtextview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.TextView;

/**
 * Created by Senh Linsh on 17/3/27.
 */

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

这个技巧对我很管用(min-sdk >= 18)。

我使用android:includeFontPadding="false"和一个像android:layout_marginTop="-11dp"和把我的TextView在一个FrameLayout(或任何ViewGroup…)

最后是示例代码:

<LinearLayout
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    >

    <TextView
        style="@style/MyTextViews.Bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        android:textSize="48sp"
        android:layout_marginTop="-11dp"
        android:includeFontPadding="false"
        tools:text="1"/>
</LinearLayout>

你可以尝试使用这个属性(ConstraintLayout):layout_constraintBaseline_toBaselineOf

是这样的:

app: layout_constraintBaseline_toBaselineOf = @ + id / textView”

添加android:includeFontPadding="false",看看是否有帮助。并使文本视图大小与文本大小相同,而不是“包装内容”。它肯定会起作用。