两种情况
1.控件外层父控件为 LinearLayout
topRightLayout.setLayoutParams(new LinearLayout.LayoutParams((int) (getDisplayWidth(this) * 0.8),
LayoutParams.FILL_PARENT));
1.控件外层父控件为 RelativeLayout
topRightLayout.setLayoutParams(new RelativeLayout.LayoutParams((int) (getDisplayWidth(this) * 0.8),
LayoutParams.FILL_PARENT));
获取屏幕宽度
public int getDisplayWidth(Context context) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
获取屏幕高度
public int getDisplayWidth(Context context) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}