设置控件宽高比例

//获取屏幕宽度
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);

//计算宽高,我需要的图片尺寸是280*136
int width = dm.widthPixels- DensityTool.dp2px(context, 10f) * 2; //乘以2是因为左右两侧的宽度
int height = (int) (width / 16 *9f ); //280*136

//根据控件类型选择使用layout
LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) holder.rl.getLayoutParams();
linearParams.width = width;//宽
linearParams.height = height;//高

holder.rl.setLayoutParams(linearParams);

你可能感兴趣的:(UI)