如何对齐文本TextView的顶部? [英] How to align the text to top of TextView?

查看:896
本文介绍了如何对齐文本TextView的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文本对齐到一个TextView顶部?
对于秋千setInsets相当于Android的API()?
这是文字的顶部应该开始在(0,0)的TextView的

How to align the text to top of a TextView?
Equivalent Android API for Swings setInsets()?
that is top of text should start be in (0,0) of TextView

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

推荐答案

因此​​,用于英语以外的字符在TextView中顶部空间的填充,如重音。要消除这种空间您可以设置安卓includeFontPadding 属性在XML或者你可以以编程方式做到这一点与功能 setIncludeFontPadding(假)

So the space at the top of the TextView is padding used for characters outside the English language such as accents. 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).

看那 SDK文档的TextView 如果这还不清楚。

Look at the SDK documentation for TextView if this is still unclear.

EDITED答案
如果设置安卓includeFontPadding 属性不完成你想要做什么,其他的解决办法是覆盖的OnDraw(帆布油画)您正在使用,以便它消除了额外的顶部填充的Andr​​oid添加到每一个TextView的,该TextView中的方法。写我原来的答复后,我注意到,由于某种原因,TextView中包括除字体填充微胖。除去字体填充的以及的,因为这额外的填充完全对齐文本到TextView的的顶部。看看下面的code段。

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. After writing my original answer, I noticed that for some reason TextView includes extra padding in addition to the font padding. Removing the font padding as well as this additional padding perfectly aligns the text to the top of the 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();
    }
} 

这篇关于如何对齐文本TextView的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆