本文总结分析了android编程获取控件宽和高的方法。分享给大家供大家参考,具体如下:
我们都知道在oncreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例:
首先我们自己写一个控件,这个控件非常简单:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class myimageview extends imageview { public myimageview(context context, attributeset attrs) { super (context, attrs); } public myimageview(context context) { super (context); } @override protected void onmeasure( int widthmeasurespec, int heightmeasurespec) { super .onmeasure(widthmeasurespec, heightmeasurespec); system.out.println( "onmeasure 我被调用了" +system.currenttimemillis()); } @override protected void ondraw(canvas canvas) { super .ondraw(canvas); system.out.println( "ondraw 我被调用了" +system.currenttimemillis()); } } |
布局文件:
1
2
3
4
5
|
<com.test.myimageview android:id= "@+id/imageview" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/test" /> |
测试的activity的oncreate():
1
2
3
4
5
6
|
@override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); system.out.println( "执行完毕.." +system.currenttimemillis()); } |
现在我们现在来看一下结果:
说明等oncreate方法执行完了,我们定义的控件才会被度量(measure),所以我们在oncreate方法里面通过view.getheight()获取控件的高度或者宽度肯定是0,因为它自己还没有被度量,也就是说他自己都不知道自己有多高,而你这时候去获取它的尺寸,肯定是不行的.
现在碰到这个问题我们不能不解决,在网上找到了如下办法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//------------------------------------------------方法一 int w = view.measurespec.makemeasurespec( 0 ,view.measurespec.unspecified); int h = view.measurespec.makemeasurespec( 0 ,view.measurespec.unspecified); imageview.measure(w, h); int height =imageview.getmeasuredheight(); int width =imageview.getmeasuredwidth(); textview.append( "\n" +height+ "," +width); //-----------------------------------------------方法二 viewtreeobserver vto = imageview.getviewtreeobserver(); vto.addonpredrawlistener( new viewtreeobserver.onpredrawlistener() { public boolean onpredraw() { int height = imageview.getmeasuredheight(); int width = imageview.getmeasuredwidth(); textview.append( "\n" +height+ "," +width); return true ; } }); //-----------------------------------------------方法三 viewtreeobserver vto2 = imageview.getviewtreeobserver(); vto2.addongloballayoutlistener( new ongloballayoutlistener() { @override public void ongloballayout() { imageview.getviewtreeobserver().removeglobalonlayoutlistener( this ); textview.append( "\n\n" +imageview.getheight()+ "," +imageview.getwidth()); } }); |
这三个方法是哪里找到现在已经忘了.
现在要讨论的是当我们需要时候使用哪个方法呢?
现在把测试的activity改成如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); final imageview imageview = (imageview) findviewbyid(r.id.imageview); //------------------------------------------------方法一 int w = view.measurespec.makemeasurespec( 0 ,view.measurespec.unspecified); int h = view.measurespec.makemeasurespec( 0 ,view.measurespec.unspecified); imageview.measure(w, h); int height =imageview.getmeasuredheight(); int width =imageview.getmeasuredwidth(); textview.append( "\n" +height+ "," +width); system.out.println( "执行完毕.." +system.currenttimemillis()); } |
接着来看下面几种方式输出结果:
把测试activity改成如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); final imageview imageview = (imageview) findviewbyid(r.id.imageview); //-----------------------------------------------方法二 viewtreeobserver vto = imageview.getviewtreeobserver(); vto.addonpredrawlistener( new viewtreeobserver.onpredrawlistener() { public boolean onpredraw() { int height = imageview.getmeasuredheight(); int width = imageview.getmeasuredwidth(); textview.append( "\n" +height+ "," +width); return true ; } }); } |
结果如下:
方法三就不再测试了同方法二!!!
那么方法而和方法三在执行上有什么区别呢?
我们在布局文件中加入一个textview来记录这个控件的宽高.
1
2
3
4
5
6
7
8
|
<scrollview android:layout_width= "wrap_content" android:layout_height= "wrap_content" > <textview android:id= "@+id/text" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> </scrollview> |
先来测试方法二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); final imageview imageview = (imageview) findviewbyid(r.id.imageview); //-----------------------------------------------方法二 viewtreeobserver vto = imageview.getviewtreeobserver(); vto.addonpredrawlistener( new viewtreeobserver.onpredrawlistener() { public boolean onpredraw() { int height = imageview.getmeasuredheight(); int width = imageview.getmeasuredwidth(); textview.append( "\n" +height+ "," +width); return true ; } }); } |
结果如下:
我们再来测试方法三
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); final imageview imageview = (imageview) findviewbyid(r.id.imageview); //-----------------------------------------------方法三 viewtreeobserver vto2 = imageview.getviewtreeobserver(); vto2.addongloballayoutlistener( new ongloballayoutlistener() { @override public void ongloballayout() { imageview.getviewtreeobserver().removeglobalonlayoutlistener( this ); textview.append( "\n\n" +imageview.getheight()+ "," +imageview.getwidth()); } }); } |
输出结果如下:
我想这方法二和方法三之间的区别就不用说了吧.
总结:那么需要获取控件的宽高该用那个方法呢?
方法一: 比其他的两个方法多了一次计算,也就是多调用了一次onmeasure()方法,该方法虽然看上去简单,但是如果要目标控件计算耗时比较大的话(如listview等),不建议使用.
方法二,它的回调方法会调用很多次,并且滑动textview的时候任然会调用,所以不建议使用.
方法三,比较合适.
当然,实际应用的时候需要根据实际情况而定.
希望本文所述对大家android程序设计有所帮助。