在android怎样正确地运用TabHost
程序代码:public class photoerActivity extends TabActivity implements OnItemClickListener, OnClickListener, ViewFactory {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.photoestabhost);
setTitle("图片鉴赏...");
initComponent();
}
private FoolImageAdapter adapter;
private GridView gridview;
private ImageButton imageButton;
private ImageSwitcher imageSwitcher;
private ImageView imageview;
private void initComponent()
{
//获取TabHost组件对象实例
TabHost tabHost = this.getTabHost();
//初始化TabHost的标签
imageButton = (ImageButton) findViewById (R.id.imageButton);
imageButton.setOnClickListener(this);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("神雕侠侣").setContent(imageButton.getId()));
imageSwitcher = (ImageSwitcher) findViewById (R.id.imageSwitcher);
imageSwitcher.setFactory(this);
imageSwitcher.setOnClickListener(this);
imageSwitcher.setImageResource(R.drawable.tl_9);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("天龙八部").setContent(imageSwitcher.getId()));
imageview = (ImageView) findViewById (R.id.imageView);
imageview.setOnClickListener(this);
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("少年包青天").setContent(imageview.getId()));
adapter = new FoolImageAdapter(this);
gridview = (GridView) findViewById (R.id.gridView);
//添加点击事件监听器
gridview.setOnItemClickListener(this);
gridview.setAdapter(adapter);
tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("倚天屠龙").setContent(gridview.getId()));
//设置当前标签页
//tabHost.setCurrentTab(1);
}
//GridView 组件点击某一个Items的响应事件程序
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// TODO Auto-generated method stub
int resId = adapter.geResId(pos);
setTitle(resId);
}
//imagebutton 按钮点击事件的响应程序
private int index_b = 0;
private int index_s = 0;
private int index_n = 0;
private final int sdPictureIds[] =
{
R.drawable.sd_1, R.drawable.sd_2, R.drawable.sd_3,
// R.drawable.sd_4, R.drawable.sd_5, R.drawable.sd_6,
// R.drawable.sd_7, R.drawable.sd_8, R.drawable.sd_9,
};
private final int tlPictureIds[] =
{
R.drawable.tl_1, R.drawable.tl_2, R.drawable.tl_3,
// R.drawable.tl_4, R.drawable.tl_5, R.drawable.tl_6,
// R.drawable.tl_7, R.drawable.tl_8, R.drawable.tl_9,
};
private final int snPictureIds[] =
{
R.drawable.sn_1, R.drawable.sn_2, R.drawable.sn_3,
// R.drawable.sn_4, R.drawable.sn_5, R.drawable.sn_6,
// R.drawable.sn_7, R.drawable.sn_8, R.drawable.sn_9,
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == imageButton.getId())
{
index_b++;
if ( index_b >= 3 )
{
index_b = 0;
}
imageButton.setImageResource(sdPictureIds[index_b]);
}
else if (v.getId() == imageSwitcher.getId())
{
index_s++;
if ( index_s >= 3)
{
index_s = 0;
}
imageSwitcher.setImageResource(tlPictureIds[index_s]);
}
else if (v.getId() == imageview.getId())
{
index_n++;
if ( index_n >= 3 )
{
index_n = 0;
}
imageview.setImageResource(snPictureIds[index_n]);
}
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView imageview = new ImageView(this);
final int padding = 16;
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageview.setPadding(padding, padding, padding, padding);
return imageview;
}
}
class FoolImageAdapter extends BaseAdapter
{
private Context mContext = null;
private int mPictureIDs[] =
{
R.drawable.yt_1, R.drawable.yt_2, R.drawable.yt_3,
// R.drawable.yt_4, R.drawable.yt_5, R.drawable.yt_6,
// R.drawable.yt_7, R.drawable.yt_8, R.drawable.yt_9,
};
FoolImageAdapter(Context c)
{
mContext = c;
}
@Override
public int getCount() {//获取图片的数量
// TODO Auto-generated method stub
return mPictureIDs.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
//获取指定位置的项目的资源ID
public int geResId(int position)
{
return mPictureIDs[position];
}
@Override//获取选择项目所对应的视图
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = null;
if (convertView == null)
{
imageView = new ImageView(mContext);
//设置图片视图属性
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mPictureIDs[position]);
return imageView;
}
}java文件



说明: 单独用一个Activity去实现 Gallery 和 imageswitcher 可以成功 但是嵌入到TabHost 就是有问题 现在还没有明白过来问题的所在