Android:TextView:删除顶部和底部的间距和填充

发布于 2024-10-14 08:29:04 字数 496 浏览 8 评论 0原文

当我有一个文本中带有 \nTextView 时,右侧有两个 singleLine TextView s,一个在另一个下面,中间没有间距。我为所有三个 TextView 设置了以下内容。

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

左侧 TextView 的第一行与右上角 TextView 完美对齐。

左侧TextView第二行比右下TextView第二行高一点。

似乎在 TextView 的顶部和底部有某种隐藏的填充。我怎样才能删除它?

When I have a TextView with a \n in the text,, on the right I have two singleLine TextViews, one below the other with no spacing in between. I have set the following for all three TextViews.

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

The first line of the left TextView lines up perfectly with the top right TextView.

The second line of the left TextView is a little higher than the second line of the bottom right TextView.

It seems that there is some kind of hidden padding on the top and the bottom of the TextViews. How can I remove that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

伪心 2024-10-21 08:29:05

简单的方法有效:

setSingleLine();
setIncludeFontPadding(false);

如果它不起作用,则尝试在代码上方添加此代码:

setLineSpacing(0f,0f);
// and set padding and margin to 0

如果您需要多行,也许您需要通过临时单行 TextView 精确计算填充顶部和底部的高度(在删除填充之前和之后) ) ,然后使用负填充或一些带有平移 Y 的幽灵布局应用降低高度结果。哈哈

Simple method worked:

setSingleLine();
setIncludeFontPadding(false);

If it not worked, then try to add this above that code:

setLineSpacing(0f,0f);
// and set padding and margin to 0

If you need multi line, maybe you'll need to calculate exactly the height of padding top and bottom via temp single line TextView (before and after remove padding) , then apply decrease height result with negative padding or some Ghost Layout with translate Y. Lol

旧竹 2024-10-21 08:29:05

唯一有效的是

android:lineSpacingExtra="-8dp"

Only thing that worked is

android:lineSpacingExtra="-8dp"
笑忘罢 2024-10-21 08:29:05

据我所知,这是大多数小部件所固有的,并且“填充”的数量因手机制造商而异。该填充实际上是图像边框和 9 补丁图像文件中的图像之间的空白区域。

例如,在我的 Droid X 上,微调器小部件比按钮有更多的空白,这使得当您有一个内联按钮的微调器时看起来很尴尬,但在我妻子的手机上,相同的应用程序没有相同的问题,而且看起来很棒!

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

啊啊,Android 的痛苦。

编辑:澄清填充与空白。

To my knowledge this is inherent to most widgets and the amount of "padding" differs among phone manufacturers. This padding is really white space between the image border and the image in the 9 patch image file.

For example on my Droid X, spinner widgets get extra white space than buttons, which makes it look awkward when you have a spinner inline with a button, yet on my wife's phone the same application doesn't have the same problem and looks great!

The only suggestion I would have is to create your own 9 patch files and use them in your application.

Ahhh the pains that are Android.

Edited: Clarify padding vs white space.

红衣飘飘貌似仙 2024-10-21 08:29:05

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

我使用了 android:includeFontPadding="false"负边距,例如 android:layout_marginTop="-11dp" 并放置了我的 TextViewFrameLayout或任何 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>

This trick worked for me (for min-sdk >= 18).

I used android:includeFontPadding="false" and a negative margin like android:layout_marginTop="-11dp" and put my TextView inside a FrameLayout ( or any ViewGroup...)

enter image description here

and finally sample codes:

<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>
沧笙踏歌 2024-10-21 08:29:05

使用constraintlayout作为根视图,然后添加指南助手。

示例:

<TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 app:layout_constraintTop_toTopOf="parent" 
 app:layout_constraintStart_toStartOf="parent" />

<TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/guideline" />

<androidx.constraintlayout.widget.Guideline
 android:id="@+id/guideline"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 app:layout_constraintGuide_begin="20dp" />

属性layout_constraintGuide_begin值只是示例,这取决于您的需求。

Use constraintlayout as your root view then add a guideline helper.

Example:

<TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 app:layout_constraintTop_toTopOf="parent" 
 app:layout_constraintStart_toStartOf="parent" />

<TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/guideline" />

<androidx.constraintlayout.widget.Guideline
 android:id="@+id/guideline"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 app:layout_constraintGuide_begin="20dp" />

Attribute layout_constraintGuide_begin value is just example, it depend on your needs.

北方。的韩爷 2024-10-21 08:29:05

这对我有用:

android:minHeight="0dp"

This worked for me:

android:minHeight="0dp"
同展鸳鸯锦 2024-10-21 08:29:05

您可以尝试使用此属性(ConstraintLayout):layout_constraintBaseline_toBaselineOf

You can try to use this attribute(ConstraintLayout): layout_constraintBaseline_toBaselineOf

も星光 2024-10-21 08:29:05

您可以参考下面的 XML 文件,其中 imageView 将从 Textview 文本基线的底部开始。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageViewHello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_star_05"
        android:baselineAlignBottom="true"
        android:layout_alignBaseline="@+id/textViewHello"
        android:layout_toRightOf="@+id/textViewHello"
        android:layout_toEndOf="@+id/textViewHello" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textViewHello"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

或者,假设对于大多数用例,您需要 imageView 的底部与基线对齐,为此您可以将以下代码放入 TextView 中。

android:layout_alignBaseline="@+id/imageViewHello"

You can take reference from the below XML file where the imageView will start from the bottom of baseline of the text of Textview.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageViewHello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_star_05"
        android:baselineAlignBottom="true"
        android:layout_alignBaseline="@+id/textViewHello"
        android:layout_toRightOf="@+id/textViewHello"
        android:layout_toEndOf="@+id/textViewHello" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textViewHello"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Alternatively, assuming for most of your use cases you would need the bottom of your imageView to be aligned with the baseline, for that you can put the below code in your TextView.

android:layout_alignBaseline="@+id/imageViewHello"
她说她爱他 2024-10-21 08:29:05

这有效:

android:padding="0sp"

“在此处输入图像描述"

This works:

android:padding="0sp"

enter image description here

恰似旧人归 2024-10-21 08:29:05

看这个:

将 ImageView 与 EditText 水平对齐

看来 EditText 的背景图片有一些透明像素也添加填充。

解决方案是将 EditText 的默认背景更改为其他内容(或者什么都不更改,但 EditText 没有背景可能是不可接受的)。这可以通过设置 android:background XML 属性来实现。

android:background="@drawable/myEditBackground"

See this:

Align ImageView with EditText horizontally

It seems that the background image of EditText has some transparent pixels which also add padding.

A solution is to change the default background of EditText to something else (or nothing, but no background for a EditText is probably not acceptable). That's can be made setting android:background XML attribute.

android:background="@drawable/myEditBackground"
樱花落人离去 2024-10-21 08:29:05

这对我有用

只需在您的 Textview 中添加两行即可

 android:includeFontPadding="false"
 安卓:lineSpacingMultiplier =“0.8”
<TextView
            android:id="@+id/searchByItemTv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:fontFamily="@font/dm_sans"
            android:includeFontPadding="false"
            android:lineSpacingMultiplier="0.8"
            android:paddingVertical="3dp"
            android:paddingStart="4dp"
            android:paddingEnd="6dp"
            android:text="@string/date_of_birth"
            android:textColor="@color/text_black"
            android:textSize="16dp" />

注意:不要使用 android:textAppearance=""

It's working for me

just add two lines to your Textview

 android:includeFontPadding="false"
 android:lineSpacingMultiplier="0.8"
<TextView
            android:id="@+id/searchByItemTv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:fontFamily="@font/dm_sans"
            android:includeFontPadding="false"
            android:lineSpacingMultiplier="0.8"
            android:paddingVertical="3dp"
            android:paddingStart="4dp"
            android:paddingEnd="6dp"
            android:text="@string/date_of_birth"
            android:textColor="@color/text_black"
            android:textSize="16dp" />

note: don't use android:textAppearance=""

妥活 2024-10-21 08:29:05

在 ImageView xml 中使用它

android:adjustViewBounds="true"

Use this in your ImageView xml

android:adjustViewBounds="true"

薄荷梦 2024-10-21 08:29:04
setIncludeFontPadding (boolean includepad)

或者在 XML 中,这将是:

android:includeFontPadding="false"

设置 TextView 是否包含额外的顶部和底部填充,以便为高于正常上升和下降的重音腾出空间。默认为 true。

setIncludeFontPadding (boolean includepad)

or in XML this would be:

android:includeFontPadding="false"

Set whether the TextView includes extra top and bottom padding to make room for accents that go above the normal ascent and descent. The default is true.

花心好男孩 2024-10-21 08:29:04

我搜索了很多正确的答案,但没有找到可以完全删除 TextView 中所有填充的答案,但最终在浏览了 官方文档解决了单行文本

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

将这两行添加到TextView xml 将完成这项工作。
第一个属性删除为重音符号保留的填充,第二个属性删除为保持两行文本之间的适当间距而保留的间距。

确保不要在多行 TextView 中添加 lineSpacingExtra="0dp",因为它可能会使外观变得笨拙

I searched a lot for proper answer but no where I could find an Answer which could exactly remove all the padding from the TextView, but finally after going through the official doc got a work around for Single Line Texts

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

Adding these two lines to TextView xml will do the work.
First attribute removes the padding reserved for accents and second attribute removes the spacing reserved to maintain proper space between two lines of text.

Make sure not to add lineSpacingExtra="0dp" in multiline TextView as it might make the appearance clumsy

涙—继续流 2024-10-21 08:29:04

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

我的解决方案? TextView 上的 layout_marginBottom="-3dp" 为您提供了底部的解决方案,
嘭!

虽然 layout_marginTop 上的 -3dp 失败了......呃。

I feel your pain. I've tried every answer above, including the setIncludeFontPadding to false, which did nothing for me.

My solution? layout_marginBottom="-3dp" on the TextView gives you a solution for the bottom,
BAM!

Although, -3dp on layout_marginTop fails....ugh.

漫漫岁月 2024-10-21 08:29:04

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

像这样:

应用:layout_constraintBaseline_toBaselineOf="@+id/textView"

在此处输入图像描述

在此处输入图像描述

You can try to use this attribute(ConstraintLayout):layout_constraintBaseline_toBaselineOf

Like this:

app:layout_constraintBaseline_toBaselineOf="@+id/textView"

enter image description here

enter image description here

暮凉 2024-10-21 08:29:04

更新的 XML

android:fontFamily="monospace"
android:includeFontPadding="false"

Updated XML

android:fontFamily="monospace"
android:includeFontPadding="false"
萝莉病 2024-10-21 08:29:04

如果您使用 AppCompatTextView (或来自 < code>API 28 开始)您可以使用这 2 个属性的组合来删除第一行上的空格:

XML

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

Kotlin

text.firstBaselineToTopHeight = 0
text.includeFontPadding = false

If you use AppCompatTextView ( or from API 28 onward ) you can use the combination of those 2 attributes to remove the spacing on the first line:

XML

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

Kotlin

text.firstBaselineToTopHeight = 0
text.includeFontPadding = false
北方的韩爷 2024-10-21 08:29:04

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

Add android:includeFontPadding="false" to see if it helps.And make text view size same as that of text size rather than "wrap content".It will definitely work.

拔了角的鹿 2024-10-21 08:29:04

这也让我很恼火,我发现的答案是字体本身实际上有额外的空间,而不是 TextView。这是相当令人恼火的,因为它来自文档发布背景,Android 对排版元素的控制有限。我建议使用可能不存在此问题的自定义字体(例如 Bitstream Vera Sans,它已获得重新分发许可)。不过,我不确定具体是否如此。

This annoyed me too, and the answer I found was that there is actually additional space in the font itself, not the TextView. It is rather irritating, coming from a document publishing background, the limited amount of control you have with Android over typographic elements. I'd recommend using a custom typeface (such as Bitstream Vera Sans, which is licensed for redistribution) that may not have this issue. I'm not sure specifically whether or not it does, though.

郁金香雨 2024-10-21 08:29:04

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

I remove the spacing in my custom view -- NoPaddingTextView.

https://github.com/SenhLinsh/NoPaddingTextView

enter image description here

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;
    }
}
因为看清所以看轻 2024-10-21 08:29:04

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

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

在此处输入图像描述

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

这是修复它的关键代码:

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

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

myTextView.setIncludeFontPadding(false);

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

在此处输入图像描述

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

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

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

没有 setTypeface(mfont);

在此处输入图像描述

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

在此处输入图像描述

不带 <代码>setIncludeFontPadding(false);:

在此处输入图像描述

没有其中 3 个(即原始图像):

在此处输入图像描述

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.

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:

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);:

enter image description here

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

enter image description here

Without setIncludeFontPadding(false);:

enter image description here

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

enter image description here

冷清清 2024-10-21 08:29:04
<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基线对齐,并将ImageView上的android:baselineAlignBottom设置为true,这将使ImageView的基线位于底部。其他视图可以使用 ImageView 的底部自行对齐,该底部本身与 TextView 的基线相同。

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

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

With an extra ImageView we can set the TextView to be baseline aligned with the ImageView and set the android:baselineAlignBottom on the ImageView to true, which will make the baseline of ImageView to bottom. Other views can align itself using the bottom of the ImageView which itself is the same as the TextView's baseline.

This however only fixes the padding bottom not the top.

等数载,海棠开 2024-10-21 08:29:04

我认为这个问题可以这样解决:

 <TextView
        android:id="@+id/leftText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="Hello World!\nhello world" />

 <TextView
        android:id="@+id/rightUpperText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/leftText"
        android:layout_alignTop="@+id/leftText"
        android:textSize="30dp"
        android:text="Hello World!" />

 <TextView
        android:id="@+id/rightLowerText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/leftText"
        android:layout_below="@+id/rightUpperText"
        android:textSize="30dp"
        android:text="hello world" />

结果如下:

ScreenShot-1

< a href="https://i.sstatic.net/q2AoM.png" rel="nofollow noreferrer">ScreenShot-3

虽然rightLowerText中的特殊字符行看起来比第二行高一点leftText,它们的基线仍然对齐。

I think this problem can be solved in this way:

 <TextView
        android:id="@+id/leftText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="Hello World!\nhello world" />

 <TextView
        android:id="@+id/rightUpperText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/leftText"
        android:layout_alignTop="@+id/leftText"
        android:textSize="30dp"
        android:text="Hello World!" />

 <TextView
        android:id="@+id/rightLowerText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/leftText"
        android:layout_below="@+id/rightUpperText"
        android:textSize="30dp"
        android:text="hello world" />

Those are the results:

ScreenShot-1

ScreenShot-3

Though the line of special characters in rightLowerText looks a little bit higher than the second line of leftText, their baselines are stilled aligned.

心清如水 2024-10-21 08:29:04

您可以使用此处列出的一些技巧(负边距、字体填充等)部分解决此问题,但您无法解决字体本身的大小[1]。字体的每个“单元格”的固定高度必须足够大,以容纳所有语言中最高的字符,包括上升部分和下降部分。

如果您采用其他答案中列出的技巧,字体可能最终会被剪切。

解决方案是向您的设计师解释这就是字体的打包方式,并让他们在设计中考虑到这一点。当它被翻译成其他语言并且上升/下降部分被剪掉时,不要现在就进行修改以将错误归档。

编写一个示例应用程序,只需将一系列字符放入 TextView 中,然后在其周围绘制一个框,更改背景或其他内容,以便您可以看到字体的自然填充。将其交给您的设计师,让其重新融入到他们的设计中。

[1] 我想人们可以用压缩的上升部分和下降部分打包自己的字体。

You can partially resolve this with some of the hacks listed here (negative margins, font padding, etc), but you can't get around the size of the font itself[1]. The fixed height of each "cell" of a font must be large enough to accommodate the tallest character, in all languages, including ascenders and descenders.

If you employ the hacks listed in other answers the font may end up clipped.

The solution is to explain to your designers that this is how fonts are packaged, and to have them account for this in their designs. Don't hack things up now to have bugs filed down the road when it's translated to other languages and ascenders / descenders are clipped.

Write a sample app the just puts a series of characters in a TextView, then draw a box around it, change the background or something so you can see the natural padding of the font. Give this to your designers to incorporate back into their designs.

[1] I suppose one could package their own fonts with compacted ascenders and descenders.

冬天的雪花 2024-10-21 08:29:04

您可能想尝试将左侧文本视图的底部与第二个右侧文本视图的底部对齐。

You might want to try aligning the bottom of the left text view with the bottom of the 2nd right text view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文