我有一个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>
其中com.test.test.TopAlignedTextView
是我为了以编程方式设置垂直对齐顶部而创建的类,这是代码:
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();
}
}
文字从顶部和底部都被切断,我不知道如何解决这个问题