private int mMonitorHeight = 0; private int mMonitorWidth = 0; private boolean bisSetScreen = false;
动态设置满屏宽度
ViewTreeObserver vto2 = monitor.getViewTreeObserver(); vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){@Overridepublic void onGlobalLayout(){if(bisSetScreen == false){mMonitorHeight = monitor.getMeasuredHeight();mMonitorWidth = monitor.getWidth();if(mMonitorHeight != 0 && mMonitorWidth != 0){monitor.setScreenSize(mMonitorWidth, mMonitorHeight);bisSetScreen = true;}}} });
LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) TextView.getLayoutParams(); //取控件textView当前的布局参数linearParams.width = 40;// 控件的宽强制设成30
linearParams.height =40;// 控件的高强制设成20TextView.setLayoutParams(linearParams);
/*** 重设 view 的宽高*/
public static void setViewLayoutParams(View view, int nWidth, int nHeight) {ViewGroup.LayoutParams lp = view.getLayoutParams();if (lp.height != nHeight || lp.width != nWidth) {lp.width = nWidth;lp.height = nHeight;view.setLayoutParams(lp);}
}
自动属性长度
ViewGroup.LayoutParams.WRAP_CONTENT
ViewGroup.LayoutParams.MATCH_PARENT
控件的边距
RelativeLayout.LayoutParams linearParams2 =(RelativeLayout.LayoutParams) top2.getLayoutParams(); //取控件textView当前的布局参数
linearParams2.topMargin =450;// 控件的边距
top2.setLayoutParams(linearParams2);
动态设置列表高度
LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) activityOneRecyclerView.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = UIUtils.dip2px(98)*6;//98是item高度。6是item数量
工具方法:
/*** 将dip或dp值转换为px值,保证尺寸大小不变*/ public static int dip2px(Context context, float dipValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dipValue * scale + 0.5f); }