//普通的设置
public static void setViewSize(View view, int width, int height){
Log.e("设置上下","宽="+width+"高="+height);
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
//根据DP来设置,其中displayMetrics必须在Activity中设置
DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
public static void setViewSizeDp(View view, int width, int height){
Log.e("设置上下","宽="+width+"高="+height);
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width,displayMetrics);
params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height,displayMetrics);
view.setLayoutParams(params);
}
网友评论