动态设置控件大小:
其中的头文件不要弄错了: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()获取。