在 Android 中旋转 TextView 并重新测量
我正在尝试使用以下代码创建一个垂直文本视图
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
在这个 类似问题(查看 kostmo 答案而不是公认的答案)中,他使用了两个计算视图大小的自定义方法(
measureHeight
和measureWidth
)。所以
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
andmeasureWidth
).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.