简体   繁体   English

如何将文本与TextView顶部对齐?

[英]How to align the text to top of TextView?

How to align the text to top of a TextView? 如何将文本与TextView顶部对齐?
Equivalent Android API for Swings setInsets()? 用于Swings setInsets()的等效Android API?
that is top of text should start be in (0,0) of TextView 文本顶部应该从TextView的(0,0)开始

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

    <TextView 
        android:id="@+id/text1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TextView" 
        android:textSize="42sp" 
        android:paddingTop="0px" 
        android:gravity="top">
    </TextView> 
</LinearLayout> 

I have used above snippet, however still output is not as expected 我已经使用了上面的代码片段,但是仍然输出不是预期的
Any ideas? 有任何想法吗?

So the space at the top of the TextView is padding used for characters outside the English language such as accents. 因此,TextView顶部的空格是用于英语之外的字符的填充,例如重音。 To remove this space you can either set the android:includeFontPadding attribute to false in your XML or you can do it programmatically with the function setIncludeFontPadding(false) . 要删除此空间,可以在XML中将android:includeFontPadding属性设置为false ,也可以使用函数setIncludeFontPadding(false)以编程方式执行此操作。

Look at the SDK documentation for TextView if this is still unclear. 如果仍然不清楚,请查看TextViewSDK文档

EDITED ANSWER 编辑答案
If setting the android:includeFontPadding attribute does not accomplish what you're trying to do, the other solution is to override the onDraw(Canvas canvas) method of the TextView that you're using so that it eliminates the additional top padding that Android adds to every TextView. 如果设置android:includeFontPadding属性没有完成你想要做的事情,另一个解决方案是覆盖你正在使用的TextView的onDraw(Canvas canvas)方法,这样它就消除了Android添加的额外顶部填充到每个TextView。 After writing my original answer, I noticed that for some reason TextView includes extra padding in addition to the font padding. 在写完我的原始答案之后,我注意到由于某种原因,除了字体填充之外,TextView还包括额外的填充。 Removing the font padding as well as this additional padding perfectly aligns the text to the top of the TextView. 删除字体填充以及此附加填充将文本完全对齐到TextView的顶部。 Look at the code snippet below. 请查看下面的代码段。

public class TopAlignedTextView extends TextView {

    // Default constructor when inflating from XML file
    public TopAlignedTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    // Default constructor override
    public TopAlignedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        setIncludeFontPadding(false); //remove the font padding
        setGravity(getGravity() | Gravity.TOP); //make sure that the gravity is set to the top
    }

    /*This is where the magic happens*/
    @Override
    protected void onDraw(Canvas canvas){
        TextPaint textPaint = getPaint(); 
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();
        canvas.save();

        //converts 5dip into pixels            
        int additionalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics());

        //subtracts the additional padding from the top of the canvas that textview draws to in order to align it with the top.            
        canvas.translate(0, -additionalPadding);
        if(getLayout() != null)
            getLayout().draw(canvas);
        canvas.restore();
    }
} 

The actual font itself has extra whitespace on the top and bottom I believe. 我相信实际的字体本身在顶部和底部有额外的空白。

Usually what I do is give the view negative margins. 通常我所做的是给出负边距。 The issue is that this looks bad when devices come with different fonts. 问题是,当设备带有不同的字体时,这看起来很糟糕。

android:layout_marginTop="-5dp"
android:layout_marginBottom="-5dp"

You will have to adjust based on the text size, bigger text sizes require more negative padding. 您必须根据文本大小进行调整,较大的文本大小需要更多的负填充。

In your .xml, write that: 在.xml中,写下:

android:gravity="center|top"

The first "center" align your line horizontally in the center and the next "top" align vertically up. 第一个“中心”将您的线水平对齐在中间,下一个“顶部”垂直对齐。

Possible, you do not need make your view based on TextView. 可能,您不需要基于TextView创建视图。 Try to extend View and write your text in onDraw() . 尝试扩展View并在onDraw()编写文本。

Look at here: https://stackoverflow.com/a/32081250/1263771 请看这里: https//stackoverflow.com/a/32081250/1263771

I think your snippet is right. 我认为你的代码是正确的。 I guess you are bothered by some pixels between top of the letter "T" and top edge of the TextView? 我猜你被字母“T”顶部和TextView顶边之间的一些像素所打扰? This space is required to render some letters that are not present in English alphabet. 此空间需要呈现一些英文字母不存在的字母。

Check that links to get idea: 检查链接以获取想法:

Im not sure if i understand what you are meaning, but the text inside the textview is going to be align to the top if you use gravity=top on the text-view. 我不确定我是否理解你的意思,但如果你在文本视图中使用gravity = top,textview中的文本将与顶部对齐。

If you put a backgrund color on you textview you might see better what is happening... 如果你在textview上添加背景颜色,你可能会看到更好的情况......

I guess you problem is that the textview is centered since you use fill_parent on both height and width on the parent LinearLayout. 我猜你的问题是textview是居中的,因为你在父LinearLayout上的高度和宽度都使用了fill_parent。 The textview seems to be the only child so try putting gravity=top on the LinearLayout... textview似乎是唯一的孩子,所以尝试将gravity = top放在LinearLayout上......

Thats the only thing i could think of... 这是我唯一能想到的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM