动态设置view宽高,首先确认当前view的父级容器,通过getLayoutParams获取父级容器的自己测量结果
//举例 父容器是 RelativeLayout
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mView.getLayoutParams();
//通过获取的对象设置宽高,及边距
layoutParams.宽高边距
//重新赋值
mView.setLayoutParams(layoutParams);
动态获取 view的尺寸 需要监听view渲染后 才能获取到view的尺寸
ViewTreeObserver viewTreeObserver = viewById1.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
//view 渲染完毕,在这获取view的尺寸
return true;
}
});