据我了解,您正在尝试在垂直线性布局中添加图像列表?
我建议您使用具有自定义布局的 ListView 和 ArrayAdapter。
这是我为帮助您而制作的示例代码:
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
List<Bitmap> bitmaps= new ArrayList<Bitmap>();
for(int i = 1; i < 12; i++) {
try {
URL imageURL = new URL("http://developer.android.com/assets/images/dac_logo.png");
Bitmap image = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
bitmaps.add(image);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
setListAdapter(new BitmapAdapter(this,R.layout.listview_item,bitmaps));
}
自定义位图适配器:
public class BitmapAdapter extends ArrayAdapter<Bitmap>{
Context context;
int layoutResourceId;
List<Bitmap> bitmaps;
public BitmapAdapter(Context context, int textViewResourceId,
List<Bitmap> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.layoutResourceId = textViewResourceId;
this.bitmaps = objects;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(this.layoutResourceId, parent, false);
ImageView iv = (ImageView) rowView.findViewById(R.id.imageView1);
iv.setImageBitmap(this.bitmaps.get(position));
return rowView;
}
}
活动主.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list">
</ListView>
listview_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />