在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文件 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;
}
}
搜索更多相关的解决方案:
android
----------------解决方案--------------------------------------------------------
.xml文件
程序代码:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@android:id/tabhost"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<LinearLayout
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background = "#FFFFFF"
>
<ImageButton
android:id = "@+id/imageButton"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/sd_3"
android:layout_gravity = "center"
/>
<ImageSwitcher
android:id = "@+id/imageSwitcher"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
/>
<ImageView
android:id = "@+id/imageView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/sn_1"
android:layout_gravity = "center"
/>
<GridView
android:id = "@+id/gridView"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:numColumns = "auto_fit"
android:verticalSpacing = "10dp"
android:horizontalSpacing = "10dp"
android:columnWidth = "90dp"
android:stretchMode = "columnWidth"
android:gravity = "center"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
<TabHost
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@android:id/tabhost"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<LinearLayout
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background = "#FFFFFF"
>
<ImageButton
android:id = "@+id/imageButton"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/sd_3"
android:layout_gravity = "center"
/>
<ImageSwitcher
android:id = "@+id/imageSwitcher"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
/>
<ImageView
android:id = "@+id/imageView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/sn_1"
android:layout_gravity = "center"
/>
<GridView
android:id = "@+id/gridView"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:numColumns = "auto_fit"
android:verticalSpacing = "10dp"
android:horizontalSpacing = "10dp"
android:columnWidth = "90dp"
android:stretchMode = "columnWidth"
android:gravity = "center"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
----------------解决方案--------------------------------------------------------
依据上面的代码得到的界面效果
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
说明:上面第一 第二 第三 标签 附图 中一个界面(同一时刻)只是显示一张图 为了可以浏览全部的图片(还有未显示的) 以某种方式(我上面写的是通过添加setOnClickListener监听器来做的 通过点击 来改变数据源)
本来是想在上面三个标签页面中各添加两个按钮 来达到浏览图片的 前翻后翻分别 因为不知道(试过几种但是老是不行 所以放弃啦)怎么增加 而自己在上面添加setOnClickListener监听器是要用来进行Activity之间的跳转的 也即是说 但我单击下图片 会跳到一个新的Activity中去 会对你点击的图片进行一个详细的介绍
另一个现象:图片放多点 就会填出错误 所以想问下大概在什么范围之间?
----------------解决方案--------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@android:id/tabhost"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<LinearLayout
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background = "#FFFFFF"
>
<TableLayout
android:id = "@+id/tab1"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<TableRow
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<ImageButton
android:id = "@+id/tab1_btprev"
android:layout_width = "15dp"
android:layout_height = "45dp"
android:layout_gravity = "center"
android:background = "#FFFFFF"
android:src = "@drawable/uparow"
/>
</TableRow>
<TableRow
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<ImageButton
android:padding = "10dp"
android:id = "@+id/imageButton"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:background = "#FFFFFF"
android:src = "@drawable/sd_3"
android:layout_gravity = "center"
/>
</TableRow>
<TableRow
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<ImageButton
android:id = "@+id/tab1_btnext"
android:layout_width = "15dp"
android:layout_height = "45dp"
android:layout_gravity = "center"
android:background = "#FFFFFF"
android:src = "@drawable/dowarow"
/>
</TableRow>
</TableLayout>
<RelativeLayout
android:id = "@+id/tab2"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<ImageSwitcher
android:id = "@+id/imageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<Gallery
android:id = "@+id/tab2_gallery"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp"
/>
</RelativeLayout>
<TableLayout
android:id = "@+id/tab3"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<TableRow
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:padding = "20dp"
>
<ImageView
android:id = "@+id/imageView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src = "@drawable/sn_1"
android:layout_gravity = "center"
/>
</TableRow>
<TableRow
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:padding = "20dp"
>
<TableLayout
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:layout_gravity = "center"
>
<TableRow>
<Button
android:id = "@+id/tab3_btprev"
android:layout_width = "60dp"
android:layout_height = "60dp"
android:background = "@drawable/leftarow"
/>
<Button
android:id = "@+id/tab3_btnext"
android:layout_width = "60dip"
android:layout_height = "60dp"
android:background = "@drawable/rightarow"
/>
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
<GridView
android:id = "@+id/gridView"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:numColumns = "auto_fit"
android:verticalSpacing = "10dp"
android:horizontalSpacing = "5dp"
android:columnWidth = "100dp"
android:stretchMode = "columnWidth"
android:gravity = "center"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
标注有颜色的地方 为了实现gallery 和 imageswitcher 的联合使用 这地方不行
----------------解决方案--------------------------------------------------------
//获取TabHost组件对象实例
TabHost tabHost = this.getTabHost();
//初始化TabHost的标签
imageButton = (ImageButton) findViewById (R.id.imageButton);
imageButton.setOnClickListener(this);
tab1_btprev = (ImageButton) findViewById (R.id.tab1_btprev);
tab1_btprev.setOnClickListener(this);
tab1_btnext = (ImageButton) findViewById (R.id.tab1_btnext);
tab1_btnext.setOnClickListener(this);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("神雕侠侣").setContent(R.id.tab1));
imageSwitcher = (ImageSwitcher) findViewById (R.id.imageSwitcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
imageSwitcher.setOnClickListener(this);
imageSwitcher.setImageResource(R.drawable.tl_9);
gallery = (Gallery)findViewById (R.id.tab2_gallery);
gadapter = new GalleryAdapter(this);
gallery.setAdapter(gadapter);
gallery.setOnItemClickListener(this);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("天龙八部").setContent(R.id.tab2));
imageview = (ImageView) findViewById (R.id.imageView);
imageview.setOnClickListener(this);
tab3_btprev = (Button) findViewById (R.id.tab3_btprev);
tab3_btnext = (Button) findViewById (R.id.tab3_btnext);
tab3_btprev.setOnClickListener(this);
tab3_btnext.setOnClickListener(this);
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("少年包青天").setContent(R.id.tab3));
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()));
----------------解决方案--------------------------------------------------------
说明: 单独用一个Activity去实现 Gallery 和 imageswitcher 可以成功 但是嵌入到TabHost 就是有问题 现在还没有明白过来问题的所在
----------------解决方案--------------------------------------------------------