您的当前位置:首页正文

Android ImageView "match_pa

来源:华佗小知识

在开发中遇到一个问题 ,在设置ImageView 的宽的熟悉为match_parent时(如下图),在某些手机上无法按要求显示

Paste_Image.png

正确的显示应该为

Paste_Image.png

而在某些机型上,(如乐视手机) 上显示的是

Paste_Image.png
解决办法是
<ImageView
    android:id="@+id/iv_open_img"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:visibility="visible"/>```
**并且在代码中加入 其中传入你要设置的ImageView**

private void adaptImageView(ImageView imageView) {
WindowManager wm = this.getWindowManager();
int screenWidth = wm.getDefaultDisplay().getWidth();
ViewGroup.LayoutParams lp = imageView.getLayoutParams();
lp.width = screenWidth;
lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
imageView.setLayoutParams(lp);
imageView.setMaxWidth(screenWidth);
imageView.setMaxHeight(screenWidth * 5);
}```