在 Android 中旋转 TextView 并重新测量

发布于 2024-12-22 00:25:21 字数 1056 浏览 1 评论 0原文

我正在尝试使用以下代码创建一个垂直文本视图

public class VerticalTextView extends TextView{

public VerticalTextView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public VerticalTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public VerticalTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
    Log.i("VerticalTextView", "onDraw Called");
    canvas.save();
    canvas.rotate(-90, getWidth() / 2, getHeight() / 2);
    super.onDraw(canvas);
    canvas.restore();

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    super.setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());

}



}

,但我发现它没有重新定义新高度的完整宽度。因此文本确实被删节了。

有什么想法吗? 米

I'm trying to create a vertical textview with the following code

public class VerticalTextView extends TextView{

public VerticalTextView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public VerticalTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public VerticalTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
    Log.i("VerticalTextView", "onDraw Called");
    canvas.save();
    canvas.rotate(-90, getWidth() / 2, getHeight() / 2);
    super.onDraw(canvas);
    canvas.restore();

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    super.setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());

}



}

however I'm finding that it is not redefining the full width in the new height. and therefore the texts is really truncated.

Any ideas?
m

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2024-12-29 00:25:21

在这个 类似问题(查看 kostmo 答案而不是公认的答案)中,他使用了两个计算视图大小的自定义方法(measureHeightmeasureWidth)。

所以 super.setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); 根据他的回答看起来是错误的。

看来你也可以复制/粘贴他的答案,我认为这就是你想要实现的目标。

In this similar question (look at kostmo answer not the accepted one), he uses two custom methods to compute the size of the View (measureHeight and measureWidth).

So super.setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); looks wrong according to his answer.

It seems you can as well copy/paste his answer, I think it is what you are trying to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文