我想在Android的oncreate方法中获得控件的大小,用网上的方法并没有获取到,大家帮我看看下面代码哪里有错
用下面的代码并没有在Logcat中获得到相关的log信息,大家帮我看看怎么回事?<Buttonandroid:id="@+id/btn1"android:text="按钮...
用下面的代码并没有在Logcat中获得到相关的log信息,大家帮我看看怎么回事?
<Button
android:id="@+id/btn1"
android:text="按钮1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"/>
btn1 = (Button) findViewById(R.id.btn1);
ViewTreeObserver vto = btn1.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int height = btn1.getMeasuredHeight();
int width = btn1.getMeasuredWidth();
Log.i("info","\nwidth: " + width + "\nheight: " + height);
return true;
}
}); 展开
<Button
android:id="@+id/btn1"
android:text="按钮1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"/>
btn1 = (Button) findViewById(R.id.btn1);
ViewTreeObserver vto = btn1.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int height = btn1.getMeasuredHeight();
int width = btn1.getMeasuredWidth();
Log.i("info","\nwidth: " + width + "\nheight: " + height);
return true;
}
}); 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询