动态设置控件大小:
其中的头文件不要弄错了:import android.view.ViewGroup.LayoutParams;
LayoutParams para = bt1.getLayoutParams();//bt1为按钮
para.width=200;//修改宽度
para.height=300;//修改高度
bt1.setLayoutParams(para); //设置修改后的布局。获取控件大小:
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
sb.measure(w, h);
int height = sb.getMeasuredHeight();
int width = sb.getMeasuredWidth(); 注:在onCreate使用getHeight()是无法获取控件高的,一直未0,因为没有绘制完成,只能在onCreate走完后才能调用getHeight()获取。
本文介绍了如何在Android应用中动态地调整按钮等控件的大小,并提供了获取控件当前尺寸的方法。需要注意的是,在onCreate方法中直接调用getHeight()可能无法正确获取到控件的高度。
877

被折叠的 条评论
为什么被折叠?



