【问题标题】:Showing edittext obliquely in android在android中倾斜显示edittext
【发布时间】:2012-06-17 11:10:12
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android android-widget android-edittext


    【解决方案1】:

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

    myEditText.setRotation(45.0f).

    【讨论】:

    • Gunnar,我不能使用这种方法,因为我的应用程序应该从 Android 2.1 版本开始运行。所以我需要一些可以在 Android 2.1 版本上运行的解决方案。请参阅我编辑的问题。
    【解决方案2】:

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

    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);
            }
    
    }
    }
    

    【讨论】:

      【解决方案3】:

      您可以旋转视图,示例:

      Vertical (rotated) label in Android
      Is it possible to write vertically in a textview in android?

      但我不确定 EditText 在编辑期间的外观。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 这就是我要问的,我已经实现了,但在这种情况下面临一些问题。请参阅问题中给出的链接。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 2023-03-23
        • 1970-01-01
        • 2011-07-09
        • 2012-07-07
        • 1970-01-01
        相关资源
        最近更新 更多