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

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

问题描述

如何将文本对齐到 TextView 的顶部?
Swings setInsets() 的等效 Android API?
文本的顶部应该开始在 TextView 的 (0,0)

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 顶部的空间是用于填充英语以外的字符,例如重音符号.要删除此空间,您可以在 XML 中将 android:includeFontPadding 属性设置为 false,或者您可以使用函数 setIncludeFontPadding(false).

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

如果仍然不清楚,请查看 TextView 的 SDK 文档.

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

编辑后的答案
如果设置 android:includeFontPadding 属性没有完成你想要做的事情,另一种解决方案是覆盖你的 TextView 的 onDraw(Canvas canvas) 方法正在使用,以便消除 Android 添加到每个 TextView 的额外顶部填充.写完我的原始答案后,我注意到出于某种原因,除了字体填充之外,TextView 还包括额外的填充.删除字体填充以及,因为这个额外的填充将文本完美地对齐到 TextView 的顶部.看看下面的代码片段.

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天全站免登陆