Imageview 按比例适应屏幕大小
DisplayMetrics dm = new DisplayMetrics();
//取得窗口属性
getWindowManager().getDefaultDisplay().getMetrics(dm);
//窗口的宽度
int screenWidth = dm.widthPixels;
ImageView mImageView = (ImageView)findViewById(R.id.login_icon); ViewGroup.LayoutParams lp = mImageView.getLayoutParams(); lp.width = screenWidth; lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT; mImageView.setLayoutParams(lp); mImageView.setMaxWidth(screenWidth); mImageView.setMaxHeight(screenWidth * 5);
需要注意:
必须设置ImageView 的属性。 android:adjustViewBounds="true"
RelativeLayout 布局是指mImageView 所在上级布局