1、先要在配置文件中用到的地方配置:
<HorizontalScrollView android:layout_width="fill_parent" android:layout_height="48dip" android:scrollbars="none"> <TableLayout android:layout_width="fill_parent" android:layout_height="48dip"> <TableRow android:id="@+id/row" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </TableLayout> </HorizontalScrollView>
这里就只显示一行,以便左右拖动。其中android:scrollbars="none"是去掉滚动条的。
2、在代码中初始化并向TableRow中添加View:
TableRow row=(TableRow) this.findViewById(R.id.row); for (int i=0;i<8;i++){ ImageView imageView=new ImageView(this); imageView.setImageResource(R.drawable.t1); row.addView(imageView); }
这儿演示了添加ImageView。