最近有这样一个需求:产品的图文详情页,完全是一张图片,且图片的高度不一定,需要页面大小随图片的高度变化
核心实现代码如下:
int screenWidth = DensityUtils.getScreenWidth(this);
//动态配置图片宽高
ViewGroup.LayoutParams lp = img.getLayoutParams();
lp.width = screenWidth;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
img.setLayoutParams(lp);
img.setMaxWidth(screenWidth);
img.setMaxHeight(screenWidth * 5);//设置最大5倍高度
if (detailUrl.length() > 0) {
Glide.with(this).load(detailUrl).placeholder(R.mipmap.placeholder_goods_detail).into(img);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:contentDescription="@string/app_name"
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</LinearLayout>
</ScrollView>
当然,你还可以用webview去解决