WindowManager wm = this.getWindowManager(); int width = wm.getDefaultDisplay().getWidth(); ImageView imageView = new ImageView(mContext); //注释部分是自适应图片的高度。单独的setAdjustViewBounds并不能起到效果,必须结合setMaxWidth和setMaxHeight才可以。 //但是有一点要明确,当图片过于高的时候,会导致图片的宽度不能填充整个屏幕而变得很窄。 //如果想要图片变得填充整个屏幕,唯一的办法就是将setMaxHeight设置的小一点。 imageView.setAdjustViewBounds(true); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(/*LinearLayout.LayoutParams.MATCH_PARENT*/ width, LinearLayout.LayoutParams.WRAP_CONTENT); System.out.println("--res--screenWidth:"+width); imageView.setLayoutParams(layoutParams); imageView.setMaxWidth(width); imageView.setMaxHeight(width * 15);// 这里其实可以根据需求而定,我这里测试为最大宽度的5倍ll_canshu_pic.setOrientation(1); ll_canshu_pic.addView(imageView);
图片自适应
最新推荐文章于 2024-06-19 10:58:02 发布