在安卓中倾斜显示编辑文本



我有一个EditText,它通常平行于屏幕X轴显示。我想倾斜显示它(与水平轴成 45 度左右)。是否可以在安卓中执行此操作。请引导我朝着一个方向前进,以便我可以尝试一下。

在通过pawelzeiba获得答案中的两个链接后,我继续解决这个问题,但再次卡住,所以我提出了另一个问题。 这是链接。

正如 Gunnar Karisson 所说,Android 3.0 中引入的 View 类中有一个 setRotation() 方法,但我不能使用它,因为我的应用程序应该在 Android 2.1 版本上运行。

所以请帮我解决这个问题。

EditText 是 View 的间接子类,它有一个旋转字段,您可以使用 setRotation(float) 设置:

myEditText.setRotation(45.0f).

经过长时间的研发,我成功地通过创建自己的自定义编辑文本来解决这个问题,该文本完全符合我的要求。

public class CustomEditText extends EditText {
private Animation rotateAnim;
public CustomEditText(Context context) {
        super(context);
}
public CustomEditText(Context context, AttributeSet attrs){
    super(context, attrs);
}
private void createAnim(Canvas canvas) {
        rotateAnim = new RotateAnimation(0, -45, 250, 50);
        rotateAnim.setRepeatCount(Animation.INFINITE);
        startAnimation(rotateAnim);
}
@Override
protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // creates the animation the first time
        if (rotateAnim == null) {
                createAnim(canvas);
        }
}
}

您可以旋转视图,例如:

安卓
中的垂直(旋转)标签是否可以在安卓的文本视图中垂直书写?

但我不确定在编辑过程中使用 EditText 会是什么样子。

如果 setRotation() 方法不适用于您正在使用的 API 级别,那么最好的办法是创建自己的 View 子类并实现 setRotation() 方法。

最新更新