Android 删除堆叠 TextViews 之间的空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10769815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:53:24  来源:igfitidea点击:

Remove space between stacked TextViews

androidandroid-layouttextview

提问by borges

I have a vertical LinearLayoutwith two TextViewinside it. The former contains a static text property (it's text never change) and the last contains a regressive timer. The image below shows both items:

我有一个垂直的LinearLayoutTextView里面有两个。前者包含一个静态文本属性(它的文本永远不会改变),最后一个包含一个回归计时器。下图显示了这两个项目:

stacked textviews

堆叠的文本视图

I want to eliminate the blank space that both texts have both top and bottom. I've tried several approaches...

我想消除两个文本都有顶部和底部的空白区域。我尝试了几种方法...

android:includeFontPadding="false"
android:lineSpacingMultiplier="1"
android:lineSpacingExtra="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"

...but none of them removed the space above the text. How can I make both texts close to each other without any extra space?

...但他们都没有删除文本上方的空间。如何在没有任何额外空间的情况下使两个文本彼此靠近?

PS: I've found this similar question, but no one answered it.

PS:我发现了这个类似的问题,但没有人回答。



Full layout code:

完整的布局代码:

<LinearLayout 
    android:id="@+id/boxTime"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp" >

    <TextView
        android:id="@+id/textRemainingTime2"
        android:layout_width="wrap_content"
        android:layout_heigh="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_horizontal"
        android:textSize="70sp"
        android:includeFontPadding="false"
        android:lineSpacingMultiplier="1"
        android:lineSpacingExtra="0dp"
        android:paddingTop="0dp"
        android:paddingBottom="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:text="@string/title" />

    <TextView
        android:id="@+id/textRemainingTime"
        android:layout_width="wrap_content"
        android:layout_heigh="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_horizontal"
        android:includeFontPadding="false"
        android:lineSpacingMultiplier="1"
        android:lineSpacingExtra="0dp"
        android:paddingTop="0dp"
        android:paddingBottom="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:textSize="107sp"
        android:text="@string/timer" />

</LinearLayout>

回答by Barak

Try using negative margins. It may take a bit of playing with the numbers to get it right, but I've done it before and it worked out well.

尝试使用负边距。可能需要花一些时间才能正确处理数字,但我以前做过,而且效果很好。

android:layout_marginTop="-5dp"

回答by Kalle

By default, a TextView includes some padding to leave space for accent characters. You can turn that off with:

默认情况下,TextView 包含一些填充来为重音字符留出空间。你可以关闭它:

 android:includeFontPadding="false"

or

或者

 textView.setIncludeFontPadding(false)

回答by Zsolt Safrany

Make baseline of the text equal to the bottom of the TextView.

使文本的基线等于 TextView 的底部。

public class BaselineTextView extends TextView {

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

    @Override
    protected void onDraw(Canvas canvas) {
        int yOffset = getHeight() - getBaseline();
        canvas.translate(0, yOffset);
        super.onDraw(canvas);
    }

}

NOTE: To avoid chopping off descenders call setClipChildren(false)on your TextView's parent ViewGroup(android:clipChildren="false"in XML).

注意:为了避免setClipChildren(false)在您TextView的父级上调用下降器ViewGroupandroid:clipChildren="false"在 XML 中)。

回答by LanDenLabs

If you set includeFontPaddingto false it helps.

如果您将includeFontPadding设置为 false 它会有所帮助。

android:includeFontPadding="false"

but if you know you don't have any descenders because you set

但是如果你知道你没有任何后代,因为你设置了

android:textAllCaps="true"

or you know there are no characters which have descender, you can make a custom TextView and set the height to the baseline.

或者您知道没有字符具有下降器,您可以制作自定义 TextView 并将高度设置为基线。

public class ExTextView extends TextView {

    public ExTextView(Context context) {
        this(context, null);
    }

    public ExTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ExTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ExTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        setHeight(getBaseline());  // <--- Shrink size to baseline
        super.onDraw(canvas);
    }
}

The source code is included in my UiComponents test app. https://github.com/landenlabs/UiComponents

源代码包含在我的 UiComponents 测试应用程序中。 https://github.com/landenlabs/UiComponents

回答by Fruit

Since my requirement is override the existing textView get from findViewById(getResources().getIdentifier("xxx", "id", "android"));, so I can't simply try onDraw()of other answer.

由于我的要求是覆盖现有的 textView get from findViewById(getResources().getIdentifier("xxx", "id", "android"));,所以我不能简单地尝试onDraw()其他答案。

But I just figure out the correct steps to fixed my problem, here is the final result from Layout Inspector:

但我只是想出了解决我的问题的正确步骤,这是布局检查器的最终结果:

enter image description here

在此处输入图片说明

Since what I wanted is merely remove the top spaces, so I don't have to choose other font to remove bottom spaces.

由于我想要的只是删除顶部空格,所以我不必选择其他字体来删除底部空格。

Here is the critical code to fixed it:

这是修复它的关键代码:

Typeface mfont = Typeface.createFromAsset(getResources().getAssets(), "fonts/myCustomFont.otf");
myTextView.setTypeface(mfont);

myTextView.setPadding(0, 0, 0, 0);

myTextView.setIncludeFontPadding(false);

The first key is set custom font "fonts/myCustomFont.otf" which has the space on bottom but not on the top, you can easily figure out this by open otf file and click any font in android Studio:

第一个键是设置自定义字体“fonts/myCustomFont.otf”,它在底部有空间但在顶部没有空间,您可以通过打开 otf 文件并单击 android Studio 中的任何字体轻松找出这一点:

enter image description here

在此处输入图片说明

As you can see, the cursor on the bottom has extra spacing but not on the top, so it fixed my problem.

如您所见,底部的光标有额外的间距,但顶部没有,因此它解决了我的问题。

The second key is you can't simply skip any of the code, otherwise it might not works. That's the reason you can found some people comment that an answer is working and some other people comment that it's not working.

第二个关键是你不能简单地跳过任何代码,否则它可能不起作用。这就是为什么您会发现有些人评论说某个答案有效,而另一些人评论说它不起作用。

Let's illustrated what will happen if I remove one of them.

让我们来说明如果我删除其中一个会发生什么。

Without setTypeface(mfont);:

没有setTypeface(mfont);

enter image description here

在此处输入图片说明

Without setPadding(0, 0, 0, 0);:

没有setPadding(0, 0, 0, 0);

enter image description here

在此处输入图片说明

Without setIncludeFontPadding(false);:

没有setIncludeFontPadding(false);

enter image description here

在此处输入图片说明

Without 3 of them (i.e. the original):

没有其中 3 个(即原始版本):

enter image description here

在此处输入图片说明

回答by Yevgeny Simkin

negative margins will do the trick

负利润率会起作用

回答by vhd

Negative Margins would do the work. You can set it by two methods -

负边距可以完成这项工作。您可以通过两种方法设置它 -

1) by xml - set the android:Layout_marginTop="-10dp"field negative

1) 通过 xml - 将android:Layout_marginTop="-10dp"字段设置为负

2) by java (Programmatically) - set the topMarginfield of LayoutParams to negative.

2)通过java(以编程方式)-将topMarginLayoutParams的字段设置为负数。

回答by HungNM2

you should change the height of TextView and maybe change android:gravity="bottom"

你应该改变 TextView 的高度,也许改变 android:gravity="bottom"

height is between textsize and size of textView with Wrapcontent.

高度介于 textsize 和带有 Wrapcontent 的 textView 的大小之间。

public class MyTextViewBounder extends TextView {
public MyTextViewBounder(Context context) {
    super(context);
}

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

public MyTextViewBounder(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    //test kích th??c th?c t? v?i kích th??c c?a Textview bao quanh text c?a nó nh? th? nào
    int width, height;
    Paint iPaint;
    Rect  iRect = new Rect();

    iPaint = new Paint();
    iPaint.setTextSize(13);
    iPaint.setTextAlign(Paint.Align.CENTER);

    //call below code outsite
    //this.setText("Hung");
    //this.setTextSize(TypedValue.COMPLEX_UNIT_PX,13);
    //this..setIncludeFontPadding(false);   //height from 18px down to 15px
    iPaint.getTextBounds("Hung",0,4,iRect); //width = 34px, height = 12px

    width = this.getWidth(); //= 30px
    height = this.getHeight(); //= 18px

    width = this.getMeasuredWidth(); //=30px
    height = this.getMeasuredHeight(); //= 18px

    width = iRect.width();  //34px
    height = iRect.height(); //12 px

}

}

}