Android 在OnCreate()中获取控件高度与宽度

试过在OnCreate()中获取控件高度与宽度的童鞋都知道,getWidth()与getHeight()方法返回是0,具体原因 看一下Activity的生命周期 就会明白。

上代码:

方法一:

<wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr>int</wbr></wbr></wbr></wbr></wbr></wbr></wbr>w=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
inth=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr>ssidtext.measure(w, h);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
intwidth=ssidtext.getMeasuredWidth();
<wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
intheight=ssidtext.getMeasuredHeight();

方法二:

<wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr>ViewTreeObservervto<span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">ssidtext.getViewTreeObserver();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>vto.addOnPreDrawListener(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,255)">new</span><span style="word-wrap:normal; word-break:normal">ViewTreeObserver.OnPreDrawListener() {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,255)">public</span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,255)">boolean</span><span style="word-wrap:normal; word-break:normal">onPreDraw() {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,255)">int</span><span style="word-wrap:normal; word-break:normal">height</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">ssidtext.getMeasuredHeight();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,255)">int</span><span style="word-wrap:normal; word-break:normal">width</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">ssidtext.getMeasuredWidth();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,255)">return</span><span style="word-wrap:normal; word-break:normal; color:rgb(0,0,255)">true</span><span style="word-wrap:normal; word-break:normal">;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>});</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

具体用哪一种可以根据需求自己选择。

你可能感兴趣的:(android)