我正在从URL动态添加图片。我正在显示图像但在图像下面我需要动态显示图像的相应名称,我正在使用自定义适配器
答案 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)
使用listview作为您的图片列表:http://developer.android.com/guide/topics/ui/layout/listview.html