HorizontalScrollView scrollView = (HorizontalScrollView) this.findViewById(R.id.scrollView); TableRow row = (TableRow) scrollView.findViewById(R.id.row); for (int i = 0; i < 5; i++) { ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.k); row.addView(imageView); } this.setContentView(scrollView);
布局文件
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView android:id="@+id/scrollView" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none"> <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:id="@+id/row" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </TableLayout> </HorizontalScrollView>