需求:输入框和文本显示框,单行的时候文字靠右,多行的时候文字靠左
最优解
public class OrientionEditText extends EditText {
public OrientionEditText (Context context) {
super(context);
}
public OrientionEditText (Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (getLineCount() > 1) {//得到文本的行数
setGravity(Gravity.LEFT);
} else {
setGravity(Gravity.RIGHT);
}
canvas.restore();
}
}