You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不使用RecyclerView,通过Glide在垂直LinearLayout中加载URL数组多张图片

Glide加载URL数组图片到垂直LinearLayout实现方案

前置依赖配置

首先在Module级build.gradle中添加Glide依赖,可替换为对应最新稳定版本:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.15.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.15.1'
    // Kotlin项目用kapt代替annotationProcessor
}

添加网络权限到AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

布局准备

主页面布局

添加垂直方向的LinearLayout作为图片承载容器,外层嵌套ScrollView解决图片过多时的滚动需求:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/ll_image_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp"/>
</ScrollView>

单张图片Item布局(可选)

如果需要统一图片样式,可新建item_image.xml布局,也可直接在代码中动态创建ImageView:

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/iv_item"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    android:layout_marginBottom="8dp"/>

代码实现

Kotlin版本

// 1. 准备图片URL数组
val imageUrlList = arrayOf(
    "https://xxx.com/img1.jpg",
    "https://xxx.com/img2.jpg",
    "https://xxx.com/img3.jpg"
)
// 2. 获取容器实例
val container = findViewById<LinearLayout>(R.id.ll_image_container)
// 3. 遍历URL加载图片
imageUrlList.forEach { url ->
    // 动态创建ImageView 也可以从item布局inflate
    val imageView = ImageView(this).apply {
        layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            dpToPx(200) // 替换为实际需要的高度
        ).also {
            it.bottomMargin = dpToPx(8)
        }
        scaleType = ImageView.ScaleType.CENTER_CROP
    }
    // 4. Glide加载图片
    Glide.with(this)
        .load(url)
        .placeholder(R.drawable.placeholder) // 占位图可选
        .error(R.drawable.error_img) // 加载错误占位图可选
        .into(imageView)
    // 5. 添加到容器
    container.addView(imageView)
}

// 扩展方法:dp转px,可选
fun Int.dpToPx(): Int {
    return (this * Resources.getSystem().displayMetrics.density).toInt()
}

Java版本

// 1. 准备图片URL数组
String[] imageUrlList = {
    "https://xxx.com/img1.jpg",
    "https://xxx.com/img2.jpg",
    "https://xxx.com/img3.jpg"
};
// 2. 获取容器实例
LinearLayout container = findViewById(R.id.ll_image_container);
// 3. 遍历URL加载图片
for (String url : imageUrlList) {
    // 动态创建ImageView
    ImageView imageView = new ImageView(this);
    // 设置布局参数
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            dpToPx(200)
    );
    params.bottomMargin = dpToPx(8);
    imageView.setLayoutParams(params);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    // 4. Glide加载图片
    Glide.with(this)
            .load(url)
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.error_img)
            .into(imageView);
    // 5. 添加到容器
    container.addView(imageView);
}

// dp转px方法
private int dpToPx(int dp) {
    return (int) (dp * getResources().getDisplayMetrics().density);
}

注意事项

  • 如果图片数量超过20张,更推荐使用RecyclerView实现,避免一次性加载大量图片导致内存占用过高
  • 可根据需求调整ImageView的缩放模式、宽高参数,也可直接在Glide的RequestOptions中配置圆角、圆形等特殊样式
  • Glide会自动绑定当前上下文的生命周期,页面销毁时自动取消未完成的加载请求,无需手动处理

内容的提问来源于stack exchange,提问作者Amine Still

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 21:15:10