用RadioGroup来自定义TabHost+Gallery应用

效果图:

 

思路:用RadioGroup来自定义Tab(当然用其他的ImageButton等也可以实现)

mainActivity代码:

  1 package com.tiantian.test;
2
3 import android.app.TabActivity;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.res.TypedArray;
7 import android.os.Bundle;
8 import android.os.Handler;
9 import android.os.Message;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.widget.BaseAdapter;
13 import android.widget.Gallery;
14 import android.widget.ImageView;
15 import android.widget.RadioButton;
16 import android.widget.RadioGroup;
17 import android.widget.TabHost;
18
19 public class TabDemoActivity extends TabActivity {
20 /** Called when the activity is first created. */
21 TabHost tab;
22 RadioGroup tab_radioGroup;
23 private int[] images = new int[]{
24 R.drawable.g1,R.drawable.g2,R.drawable.g3,
25 R.drawable.g4,R.drawable.g5,R.drawable.g6
26 };
27 @Override
28 public void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30 setContentView(R.layout.main);
31 Gallery gallery = (Gallery)findViewById(R.id.gallery);
32 gallery.setAdapter(new ImageAdapter(this));
33
34 tab = getTabHost();
35 tab.addTab(tab.newTabSpec("Spec-TabList")
36 .setIndicator("Ind-TabList")
37 .setContent(new Intent(this, TabList.class)));
38 tab.addTab(tab.newTabSpec("Spec-secondDemo")
39 .setIndicator("Ind-secondDemo")
40 .setContent(new Intent(this, secondDemo.class)));
41 tab.addTab(tab.newTabSpec("Spec-thirdDemo")
42 .setIndicator("Ind-thirdDemo")
43 .setContent(new Intent(this, thirdDemo.class)));
44 tab.addTab(tab.newTabSpec("Spec-fourthDemo")
45 .setIndicator("Ind-fourthDemo")
46 .setContent(new Intent(this, fourthDemo.class)));
47 tab.addTab(tab.newTabSpec("Spec-fifthDemo")
48 .setIndicator("Ind-fifthDemo")
49 .setContent(new Intent(this, fifthDemo.class)));
50
51 tab_radioGroup = (RadioGroup)findViewById(R.id.tab_radiogroup);
52 tab_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
53
54 @Override
55 public void onCheckedChanged(RadioGroup group, int checkedId) {
56 // TODO Auto-generated method stub
57 switch(checkedId){
58 case R.id.radioButton0:
59 tab.setCurrentTabByTag("Spec-TabList");
60 break;
61 case R.id.radioButton1:
62 tab.setCurrentTabByTag("Spec-secondDemo");
63 break;
64 case R.id.radioButton2:
65 tab.setCurrentTabByTag("Spec-thirdDemo");
66 break;
67 case R.id.radioButton3:
68 tab.setCurrentTabByTag("Spec-fourthDemo");
69 break;
70 case R.id.radioButton4:
71 tab.setCurrentTabByTag("Spec-fifthDemo");
72 break;
73 }
74 }
75 });
76 //使他初始化后聚焦在第一个RadioGroup对象
77 ((RadioButton) tab_radioGroup.getChildAt(0)).toggle();
78
79 }
80
81 public class ImageAdapter extends BaseAdapter{
82 private Context context;
83 // private int[] images = new int[]{
84 // R.drawable.g1,R.drawable.g2,R.drawable.g3,
85 // R.drawable.g4,R.drawable.g5,R.drawable.g6
86 // };
87 private int mGalleryItemBackground;
88 public ImageAdapter(Context context) {
89 super();
90 this.context = context;
91 TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
92 mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
93 a.recycle();
94 }
95
96 @Override
97 public int getCount() {
98 // TODO Auto-generated method stub
99 return images.length;
100 }
101
102 @Override
103 public Object getItem(int arg0) {
104 // TODO Auto-generated method stub
105 return images[arg0];
106 }
107
108 @Override
109 public long getItemId(int position) {
110 // TODO Auto-generated method stub
111 return position;
112 }
113
114 @Override
115 public View getView(int position, View convertView, ViewGroup parent) {
116 // TODO Auto-generated method stub
117 ImageView image = new ImageView(context);
118 image.setImageResource(images[position]);
119 image.setScaleType(ImageView.ScaleType.FIT_XY);
120 image.setLayoutParams(new Gallery.LayoutParams(120, 100));
121 image.setBackgroundResource(mGalleryItemBackground);
122 return image;
123 }
124
125 }
126
127 }

对应的布局文件:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <TabHost
3    android:id="@android:id/tabhost"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 xmlns:android="http://schemas.android.com/apk/res/android">
7
8 <LinearLayout
9    android:orientation="vertical"
10 android:layout_width="fill_parent"
11 android:layout_height="fill_parent">
12 <Gallery
13       android:id="@+id/gallery"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 />
17
18 <TabWidget
19     android:id="@android:id/tabs"
20 android:visibility="gone"
21 android:layout_width="fill_parent"
22 android:layout_height="wrap_content"
23 android:layout_weight="0.0" />
24 <FrameLayout
25         android:id="@android:id/tabcontent"
26 android:layout_width="fill_parent"
27 android:layout_height="0.0dp"
28 android:layout_weight="1.0" />
29
30 <RadioGroup
31         android:id="@+id/tab_radiogroup"
32 android:orientation="horizontal"
33 android:background="@drawable/tab_widget_background"
34 android:layout_width="fill_parent"
35 android:layout_height="wrap_content"
36 android:padding="2dp"
37 android:gravity="center_vertical"
38 >
39 <RadioButton
40           android:id="@+id/radioButton0"
41 android:text="评分"
42 android:drawableTop="@drawable/tab_icon1"
43 style="@style/tab_item_background"
44 />
45 <RadioButton
46           android:id="@+id/radioButton1"
47 android:text="紫色"
48 android:drawableTop="@drawable/tab_icon2"
49 style="@style/tab_item_background"
50 />
51 <RadioButton
52           android:id="@+id/radioButton2"
53 android:text="红色"
54 android:drawableTop="@drawable/tab_icon3"
55 style="@style/tab_item_background"
56 />
57 <RadioButton
58           android:id="@+id/radioButton3"
59 android:text="蓝色"
60 android:drawableTop="@drawable/tab_icon4"
61 style="@style/tab_item_background"
62 />
63 <RadioButton
64          android:id="@+id/radioButton4"
65 android:text="绿色"
66 android:drawableTop="@drawable/tab_icon5"
67 style="@style/tab_item_background"
68 />
69 </RadioGroup>
70
71 </LinearLayout>
72
73 </TabHost>
posted @ 2012-02-24 11:06  天天_byconan  阅读(7127)  评论(0编辑  收藏  举报