如何使用自定义适配器动态加载带有图像名称的图像

时间:2015-01-21 10:06:53

标签: java android

我正在从URL动态添加图片。我正在显示图像但在图像下面我需要动态显示图像的相应名称,我正在使用自定义适配器

2 个答案:

答案 0 :(得分:0)

试试这个xml:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/imageName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

编程:

private View addImageViewWithText(Bitmap bitmap, String text) {
    LinearLayout layout = new LinearLayout(this);// 'this' is a Context instance
    layout.setOrientation(LinearLayout.VERTICAL);

    ImageView imageView = new ImageView(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.weight = 1;
    imageView.setLayoutParams(params);
    imageView.setImageBitmap(bitmap);

    layout.addView(imageView);

    TextView textView = new TextView(this);
    params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    //params.gravity = Gravity.CENTER;
    textView.setLayoutParams(params);
    textView.setText(text);

    layout.addView(textView);

    return layout;
}

在适配器上。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ...Code to get the view holder...

    holder.imgscrollchild.removeAllViews();
    for(int i=0; i<numOfImageViews;i++) {

        holder.imgscrollchild.addView(createimg(tempValues.getImage(), tempValues.getText()));
    }

    return horizontalLayout;
}

注意:强烈建议您使用ViewHolder

答案 1 :(得分:0)