Android自定义TextView忽略布局边缘

时间:2016-01-13 13:49:23

标签: android

我的应用程序需要在可滚动的日历视图中旋转Textview。问题是在滚动内部布局后它绕过边缘。旋转静态Textview很简单但是如何解决这个绘图问题呢?谢谢!

enter image description here

我的自定义TextView代码是:

public class VerticalTextView extends TextView {

    final boolean topDown;

    public VerticalTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        final int gravity = getGravity();
        if (Gravity.isVertical(gravity)
                && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
            setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK)
                    | Gravity.TOP);
            topDown = false;
        } else
            topDown = true;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b) {
        return super.setFrame(l, t, l + (b - t), t + (r - l));
    }

    @Override
    public void draw(Canvas canvas) {

        if (topDown) {
            canvas.translate(getWidth(), 0);
            canvas.rotate(90);
        } else {
            canvas.translate(0, getHeight());
            canvas.rotate(-90);
        }

        canvas.clipRect(0, 0, getWidth(), getHeight(),
                android.graphics.Region.Op.REPLACE);
        super.draw(canvas);

    }
}

使用: View.setRotation(270);View.setRotation(-90); 浅灰色区域是TextView。正如你所看到的那样,它没有延伸到整个白色区域。

enter image description here

1 个答案:

答案 0 :(得分:0)

旋转TextView有一种更简单的方法 - 只需拨打View.setRotation()即可!然后,您根本不需要处理自定义TextView