java 自定义视图的 getWidth() 和 getHeight() 给出错误的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16969841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getWidth() and getHeight() for custom view give wrong values
提问by Rookatu
I've got a Custom View where I draw a grid onto a canvas. I want the grid to be drawn so that the outer edges of the grid coincide with the edges of the device screen. I use the getWidth()
and getHeight()
methods to determine the dimensions of my grid and then draw it, but the grid that appears always has cells drawn off-screen.
我有一个自定义视图,可以在画布上绘制网格。我希望绘制网格,使网格的外边缘与设备屏幕的边缘重合。我使用getWidth()
和getHeight()
方法来确定网格的尺寸然后绘制它,但出现的网格总是在屏幕外绘制单元格。
If I get the width and height of the display and use those for drawing instead then the width of my grid will be what I want, but the height is still off because some the height of the display is taken up by battery and wifi indicators, etc. Also, I don't want to use these values as I want to be able to embed my view in larger xml layouts.
如果我获得显示器的宽度和高度并使用它们来绘图,那么我的网格的宽度将是我想要的,但高度仍然关闭,因为显示器的某些高度被电池和 wifi 指示灯占用,等等。此外,我不想使用这些值,因为我希望能够将我的视图嵌入到更大的 xml 布局中。
Below is the code where I find my view width and height in my custom view:
下面是我在自定义视图中找到视图宽度和高度的代码:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
cellWidth = w/nCols;
cellHeight = h/nRows;
//find view dimensions
viewWidth = getWidth(); //this is just equal to w
viewHeight = getHeight(); //this is just equal to h
super.onSizeChanged(w,h,oldw,oldh);
}
and the onDraw of my custom view:
以及我的自定义视图的 onDraw:
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawRect(0,0,viewWidth,viewHeight, background);
for(int i=0; i <= nRows; i++){
canvas.drawLine(0,i*cellHeight, nCols*cellWidth,i*cellHeight,lines);
canvas.drawLine(i*cellWidth, 0, i*cellWidth, nRows*cellHeight, lines);
}
}
}
}
The approach I've followed is similar to thisbut has not worked. How can I get the true values for the width and height of my view? Thanks for reading!
我遵循的方法与此类似,但没有奏效。如何获得视图宽度和高度的真实值?谢谢阅读!
采纳答案by James Manes
I'm guessing you have already seen this?
我猜你已经看过这个了?
Or perhaps this is a different problem.
或者,这可能是一个不同的问题。
回答by dberm22
I would do it by subtracting the height of the status bar.
我会通过减去状态栏的高度来实现。
Have you seen this post?
你看过这个帖子吗?
Author suggests:
作者建议:
Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
or alternatively,
或者,
public int getStatusBarHeight()
{
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height","dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
回答by Martin Cazares
When it comes to canvas there's several things that could be causing the issue, but just at first glance i can notice that you are trying to use the getWidth before the onSizeChanged has been passed to the superclass to do the proper calculations, in stead of calling super.onSizeChanged(w,h,oldw,oldh);
at the end of the method, try by doing it like this:
当谈到画布时,有几件事可能会导致这个问题,但乍一看,我可以注意到您正在尝试在 onSizeChanged 被传递给超类之前使用 getWidth 来进行正确的计算,而不是调用super.onSizeChanged(w,h,oldw,oldh);
在方法结束时,尝试这样做:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
super.onSizeChanged(w,h,oldw,oldh);
cellWidth = w/nCols;
cellHeight = h/nRows;
//find view dimensions
viewWidth = getWidth(); //this is just equal to w
viewHeight = getHeight(); //this is just equal to h
}
This is what i saw at first glance, but truth is that when it comes to canvas you have to be very explicit on sizes by your self, and do not expect the parent to handle any size based on your draws...
这是我第一眼看到的,但事实是,当涉及到画布时,您必须非常明确自己的尺寸,并且不要指望父母根据您的绘制处理任何尺寸......
Regards!
问候!