您的位置:首页 > 其它

安卓开发之控件的大小获取和动态改变

2016-11-23 14:02 351 查看
动态设置控件大小:

其中的头文件不要弄错了: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()获取。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: