前言:在对一些控件的操作或者某些情况下,需要获取控件的宽高和手机屏幕的宽高,在查看了网上的一些方法后,自己整理了一下。截止2017/03/08,Android Studio,下列方法可用!
一:获取控件的宽度和高度: 在onCreate方法中,控件尚未进行绘制,所以,控件的getWidth()方法和getHeight()方法返回的值为0.
Button button= (Button) findViewById(R
.id.btn_child)
int w=View
.MeasureSpec.makeMeasureSpec(
0,View
.MeasureSpec.UNSPECIFIED)
int h=View
.MeasureSpec.makeMeasureSpec(
0,View
.MeasureSpec.UNSPECIFIED)
button
.measure(w,h)
int height=button
.getMeasuredHeight()
int width=button
.getMeasuredWidth()
Log
.d(
"sbsbsbsb",
"Button height:"+height+
" Button width:"+width)
二:获取屏幕的宽高:
Display display=getWindowManager()
.getDefaultDisplay()
Point size=new Point()
display
.getSize(size)
Log
.d(
"sbsbsbsb",
"width:"+size
.x+
" height:"+size
.y)
转载请注明原文地址: https://ju.6miu.com/read-6079.html