1 布局文件的设置,如下 2 3 <?xml version="1.0" encoding="utf-8"?> 4 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 5 android:id="@+id/tabhosts" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:layout_weight="1" 9 android:background="#fff" > 10 11 <LinearLayout 12 android:layout_width="fill_parent" 13 android:layout_height="match_parent" 14 android:orientation="vertical" > 15 16 <FrameLayout 17 android:id="@android:id/tabcontent" 18 android:layout_width="match_parent" 19 android:layout_weight="1.0" 20 android:layout_height="0.0dp" /> 21 22 <TabWidget 23 android:visibility="gone" 24 android:id="@android:id/tabs" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:layout_gravity="bottom" 28 android:orientation="horizontal"/> 29 30 <RadioGroup 31 android:layout_gravity="bottom" 32 android:gravity="center" 33 android:id="@+id/main_rdg" 34 android:background="@drawable/main_radio_bg" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:orientation="horizontal" > 38 39 <RadioButton 40 style="@style/bottom_radio_style" 41 android:id="@+id/rd_index" 42 android:layout_width="0dp" 43 android:layout_height="match_parent" 44 android:text="@string/index" 45 android:gravity="center_horizontal" 46 android:drawableTop="@drawable/icon_tab_homepage_checked" 47 android:layout_weight="1" 48 /> 49 50 <RadioButton 51 style="@style/bottom_radio_style" 52 android:id="@+id/rd_near" 53 android:layout_width="0dp" 54 android:layout_height="match_parent" 55 android:layout_weight="1" 56 android:text="@string/near" 57 android:drawableTop="@drawable/icon_tab_nearby_checked" /> 58 59 <RadioButton 60 style="@style/bottom_radio_style" 61 android:id="@+id/rd_own" 62 android:layout_width="0dp" 63 android:layout_height="match_parent" 64 android:layout_weight="1" 65 android:text="@string/own" 66 android:drawableTop="@drawable/icon_tab_mine_checked"/> 67 68 <RadioButton 69 style="@style/bottom_radio_style" 70 android:id="@+id/rd_more" 71 android:text="@string/more" 72 android:drawableTop="@drawable/icon_tab_more_checked" 73 android:layout_width="0dp" 74 android:layout_height="match_parent" 75 android:layout_weight="1" /> 76 </RadioGroup> 77 78 </LinearLayout> 79 </TabHost> 80 81 通过样式文件更改radioButton的效果 82 83 <?xml version="1.0" encoding="utf-8"?> 84 85 <resources xmlns:android="http://schemas.android.com/apk/res/android"> 86 87 <style name="bottom_radio_style"> 88 89 <item name="android:button">@null</item> 90 91 <item name="android:gravity">center</item> 92 93 <item name="android:background">@drawable/main_bottom_rd</item> 94 95 </style> 96 97 </resources> 98 99 选择效果(选择一个标签是的一个选择效果) 100 101 <?xml version="1.0" encoding="utf-8"?> 102 103 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 104 105 <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/main_radio_select"></item> 106 107 <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/main_radio_select"></item> 108 109 </selector> 110 111 activity中的代码设置如下: 112 113 package cn.liu.activity; 114 public class MainActivity extends ActivityGroup { 115 protected static final String TAG = "MainActivity"; 116 private TabHost tabHost = null; 117 private RadioGroup rGroup; 118 119 @Override 120 protected void onCreate(Bundle savedInstanceState) { 121 super.onCreate(savedInstanceState); 122 // 设置手机全屏显示 123 requestWindowFeature(Window.FEATURE_NO_TITLE); 124 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 125 WindowManager.LayoutParams.FLAG_FULLSCREEN); 126 setContentView(R.layout.main); 127 // 通过findviewbyId这种方式找到tabhost,就必须要调用 128 // setup方法,而且如果当前类,没有集成ActivityGroup,会报E/AndroidRuntime(380): java.lang.RuntimeException: Unable to start activity 129 ComponentInfo{cn.liu.eat/cn.liu.activity.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void 130 setup(LocalActivityManager activityGroup)且setup必须要设置成this.getLocalActivityManager()这样才不会出问题 131 tabHost = (TabHost) this.findViewById(R.id.tabhosts); 132 tabHost.setup(this.getLocalActivityManager()); 133 TabSpec tabSpec1 = tabHost.newTabSpec("tab1").setIndicator("tab1") 134 .setContent(new Intent(this, HomeActivity.class)); 135 TabSpec tabSpec2 = tabHost.newTabSpec("tab2").setIndicator("tab2") 136 .setContent(new Intent(this, NearActivity.class)); 137 TabSpec tabSpec3 = tabHost.newTabSpec("tab3").setIndicator("tab3") 138 .setContent(new Intent(this, MineActivity.class)); 139 TabSpec tabSpec4 = tabHost.newTabSpec("tab4").setIndicator("tab4") 140 .setContent(new Intent(this, MoreActivity.class)); 141 142 tabHost.addTab(tabSpec1); 143 tabHost.addTab(tabSpec2); 144 tabHost.addTab(tabSpec3); 145 tabHost.addTab(tabSpec4); 146 tabHost.setCurrentTab(0); 147 148 rGroup = (RadioGroup) this.findViewById(R.id.main_rdg); 149 rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 150 @Override 151 public void onCheckedChanged(RadioGroup group, int checkedId) { 152 switch (checkedId) { 153 case R.id.rd_index: 154 tabHost.setCurrentTabByTag("tab1"); 155 break; 156 case R.id.rd_near: 157 tabHost.setCurrentTabByTag("tab2"); 158 break; 159 case R.id.rd_own: 160 tabHost.setCurrentTabByTag("tab3"); 161 break; 162 case R.id.rd_more: 163 tabHost.setCurrentTabByTag("tab4"); 164 break; 165 } 166 } 167 }); 168 169 } 170 171 } 172