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

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

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

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

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


当前回答

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/baselineImage"
        android:includeFontPadding="false" />

    <ImageView
        android:id="@+id/baselineImage"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:baselineAlignBottom="true"
        android:layout_alignParentBottom="true" />

    <!-- This view will be exactly 10dp below the baseline of textView -->
    <View
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/baselineImage" />

</RelativeLayout>

有了额外的ImageView,我们可以设置TextView基线对齐的ImageView和设置android:baselineAlignBottom上的ImageView为真,这将使ImageView基线底部。其他视图可以使用ImageView的底部来对齐自己,ImageView本身与TextView的基线相同。

然而,这只修复填充底部而不是顶部。

其他回答

在imageviewxml中使用这个

android: adjustViewBounds = " true "

据我所知,这是大多数小部件固有的,不同的手机制造商“填充”的数量不同。这个填充实际上是图像边界和9补丁图像文件中的图像之间的空白。

例如,在我的Droid X上,旋转窗口小部件比按钮有更多的空白,这让你在旋转窗口内嵌一个按钮时看起来很尴尬,但在我妻子的手机上,同样的应用程序没有同样的问题,看起来很棒!

我唯一的建议是创建自己的9个补丁文件,并在应用程序中使用它们。

啊,Android的痛苦。

编辑:澄清填充和空白。

你可以用这里列出的一些技巧来部分解决这个问题(负边距,字体填充等),但你不能绕过字体本身的大小[1]。在所有语言中,字体的每个“单元格”的固定高度必须足以容纳最高的字符。

如果你采用了其他答案中列出的技巧,字体可以被剪掉。

解决方案是向你的设计师解释字体是如何包装的,并让他们在设计中考虑到这一点。不要因为在翻译到其他语言时出现bug而现在就进行修改。

我想人们可以用压缩升序和降序来打包他们自己的字体。

如果你使用AppCompatTextView(或者从API 28开始),你可以使用这两个属性的组合来删除第一行的空格:

XML

android:firstBaselineToTopHeight="0dp"
android:includeFontPadding="false"

科特林

text.firstBaselineToTopHeight = 0
text.includeFontPadding = false

我删除间距在我的自定义视图- 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;
    }
}