Android TextView可以';t对齐文本顶部

Android TextView可以';t对齐文本顶部,android,textview,Android,Textview,我有一个TextView,它没有对齐文本顶部,只是将其剪切掉,这是我的xml结构: <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="30dp" andr

我有一个
TextView
,它没有对齐文本顶部,只是将其剪切掉,这是我的xml结构:

<LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="30dp"
                    android:layout_marginLeft="45dp"
                    android:layout_marginRight="45dp" >

                    <CheckBox
                        android:id="@+id/privacyBtn"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:button="@drawable/login_checkbox" />

                    <com.test.test.TopAlignedTextView
                        android:id="@+id/privacyText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:gravity="top"
                        android:includeFontPadding="false"
                        android:text="Ho preso visione e acconsento al trattamento dei dati personali ai sensi dell’art. 13 d. lgs. 196/2003"
                        android:textColor="@color/lowBlack"
                        android:textSize="14sp" />
                </LinearLayout>
这就是我得到的结果:


文本从顶部和底部都被截断,我不知道如何解决这个问题

为textView设置自定义高度并测试它,对于ex 200dp,您是否尝试过xml ony方法?您不应该仅使用自定义TextView元素来设置布局重力。@StackDiego是的,我已经尝试过仅使用xml,但没有结果,这就是为什么我尝试使用自定义classI的原因。我认为在您执行此操作时会发生“canvas.translate(0,-additionalPadding);”尽量不要删除额外的padding@Signo也许不是线性布局有问题,但在他的父亲或兄弟姐妹身上,我将你的布局复制粘贴到了一个新的空白xml文件中,问题没有出现
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();
    }
}