Android 以编程方式获取布局的高度和宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21926644/
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
Get height and width of a layout programmatically
提问by Rohit
I have designed an layout in which LinearLayout
has 2 children LinearLayout
and FrameLayout
and in each child I put different views.
我设计了一个布局,其中LinearLayout
有 2 个孩子LinearLayout
,FrameLayout
并且在每个孩子中我放置了不同的视图。
I just wanted to measure height and width of FrameLayout
so that I could serve my purpose.
我只是想测量 的高度和宽度,FrameLayout
以便我可以达到我的目的。
In program I am using
在我使用的程序中
int height,width;
FrameLayout fr=(FrameLayout)findviewbyid(R.id.fr);
height=fr.getHeight();
width=fr.getWidth();
returns value as 0
for both
0
两者都 返回值
Even I tried with following code int height,width;
即使我尝试使用以下代码 int height,width;
FrameLayout fr=(FrameLayout)findviewbyid(R.id.fr);
height=fr.getMeasuredHeight();
width=fr.getMeasuredWidth();
returns same value 0
返回相同的值 0
and finally I tried with following code, int height,width;
最后我尝试使用以下代码,int height,width;
FrameLayout fr=(FrameLayout)findviewbyid(R.id.fr);
height=fr.getgetLayoutParams().height();
width=fr.getLayoutParams().width;
returns me -2
and -1
还我-2
和-1
I want the solution to get height and width of any layout programmatically?
我希望解决方案以编程方式获取任何布局的高度和宽度?
回答by Martin Cazares
The view itself, has it's own life cycle which is basically as follows:
视图本身有自己的生命周期,基本如下:
Attached
Measured
Layout
Draw
随附的
实测
布局
画
So, depending on when are you trying to get the width/height you might not see what you expect to see, for example, if you are doing it during onCreate, the view might not even been measured by that time, on the other hand if you do so during onClick method of a button, chances are that by far that view has been attached, measured, layout, and drawn, so, you will see the expected value, you should implement a ViewTreeObserver to make sure you are getting the values at the proper moment.
因此,根据您何时尝试获取宽度/高度,您可能看不到您期望看到的内容,例如,如果您在 onCreate 期间执行此操作,另一方面,当时甚至可能还没有测量该视图如果您在按钮的 onClick 方法期间这样做,很可能到目前为止该视图已被附加、测量、布局和绘制,因此,您将看到预期值,您应该实现一个 ViewTreeObserver 以确保您获得在适当的时候值。
LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
this.layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});
回答by Julian
As CapDroid say, you are trying to get the size before the view drawn, another possible solution could be to do a Post with a runnable on the View:
正如 CapDroid 所说,您正在尝试在绘制视图之前获取大小,另一种可能的解决方案是在视图上执行带有可运行的 Post:
mView.post(new Runnable() {
@Override
public void run() {
int width = mView.getWidth();
int height = mView.getHeight();
}
}
回答by Niranj Patel
I think you are getting Height and Width in OnCreate() method that's why you getting always 0 values because at that time your view still not created.
我认为您在 OnCreate() 方法中获得 Height 和 Width 这就是为什么您总是获得 0 值的原因,因为当时您的视图仍未创建。
Try to get height and width on some button click or else..
尝试在单击某些按钮时获取高度和宽度,否则..
回答by Jean-Baptiste Leduc
If you get -1 or -2 it could also be the numeric value of LayoutParams.WRAP_CONTENT (-2) or LayoutParams.MATCH_PARENT (-1).
如果你得到 -1 或 -2,它也可能是 LayoutParams.WRAP_CONTENT (-2) 或 LayoutParams.MATCH_PARENT (-1) 的数值。
回答by Siddhesh Shirodkar
just give your height and You will get height or even width if wanted.
只需给出您的高度,如果需要,您将获得高度甚至宽度。
/** * Get the view height before the view will render * @param view the view to measure * @return the height of the view */
/** * 获取视图渲染前的视图高度 * @param view 要测量的视图 * @return 视图的高度 */
public static int getViewHeight(View view) {
WindowManager wm =
(WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int deviceWidth;
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){
Point size = new Point();
display.getSize(size);
deviceWidth = size.x;
} else {
deviceWidth = display.getWidth();
}
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthMeasureSpec, heightMeasureSpec);
return view.getMeasuredHeight(); // view.getMeasuredWidth();
}
回答by Dan Bray
In Kotlin you can simply do this:
在 Kotlin 中,您可以简单地执行以下操作:
fr.post {
height=fr.height
width=fr.width
}
回答by Asaf
I had same issue but didn't want to draw on screen before measuring so I used this method of measuring the view before trying to get the height and width.
我有同样的问题,但不想在测量之前在屏幕上绘制,所以我在尝试获取高度和宽度之前使用了这种测量视图的方法。
Example of use:
使用示例:
layoutView(view);
int height = view.getHeight();
//...
void layoutView(View view) {
view.setDrawingCacheEnabled(true);
int wrapContentSpec =
MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
view.measure(wrapContentSpec, wrapContentSpec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
}
回答by Chathura Jayanath
Most easiest way is to use ViewTreeObserver, if you directly use .height or .width you get values as 0, due to views are not have size until they draw on our screen. Following example will show how to use ViewTreeObserver
最简单的方法是使用 ViewTreeObserver,如果你直接使用 .height 或 .width 你得到的值为 0,因为视图在我们的屏幕上绘制之前没有大小。下面的例子将展示如何使用 ViewTreeObserver
ViewTreeObserver viewTreeObserver = YOUR_VIEW_TO_MEASURE.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
YOUR_VIEW_TO_MEASURE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int viewHeight = YOUR_VIEW_TO_MEASURE.getHeight();
int viewWeight = YOUR_VIEW_TO_MEASURE.getWidth();
}
});
}
if you need to use this on method, use like this and to save the values you can use globle variables.
如果你需要在方法上使用这个,像这样使用并保存你可以使用全局变量的值。
回答by jmorales
try with this metods:
试试这个方法:
int width = mView.getMeasuredWidth();
int height = mView.getMeasuredHeight();
Or
或者
int width = mTextView.getMeasuredWidth();
int height = mTextView.getMeasuredHeight();
回答by Aziz
As I just ran into this problem thought I would write it for kotlin,
当我刚遇到这个问题时,我以为我会为 kotlin 编写它,
my_view.viewTreeObserver.addOnGlobalLayoutListener {
// here your view is measured
// get height using my_view.height
// get width using my_view.width
}
or
或者
my_view.post {
// here your view is measured
// get height using my_view.height
// get width using my_view.width
}