運行效果圖
新建佈局.xml,以及相應的.java
例如:activity_one.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5c3317"
android:gravity="top|center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是好友的activity" />
</LinearLayout>
OneActivity.java文件
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class OneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
}
<span style="white-space:pre"> </span>//只繼承
}<strong><span style="font-size:18px;">
</span></strong>
activity_one.xml,activity_two.xml,activity_three.xml,activity_four.xml,activity_five.xml對應的文件
OneActivity.java,TwoActivity.java,ThreeActivity.java,FourActivity.java,FiveActivity.java,都如上相同只不過內容自定義不同
activity_main.xml修改如下:
<!-- 去除前面的多選框 -->
<style name="style">
<item name="android:button">@null</item>
</style>
然後再修改主佈局文件
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone"/>
<RadioGroup
android:id="@+id/main_radio"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_gravity="bottom"
android:background="@drawable/bg"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rbtn_1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:tag="rbtn_1"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/bg_1"
style="@style/style"
android:layout_weight="1.0"
android:gravity="center"
android:text="好友"/>
<RadioButton
android:id="@+id/rbtn_2"
android:layout_height="match_parent"
android:layout_width="match_parent"
style="@style/style"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/bg_2"
android:tag="rbtn_2"
android:layout_weight="1.0"
android:gravity="center"
android:text="廣場" />
<RadioButton
android:id="@+id/rbtn_3"
android:layout_height="match_parent"
android:layout_width="match_parent"
style="@style/style"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/bg_3"
android:layout_weight="1.0"
android:tag="rbtn_3"
android:gravity="center"
android:text="首頁" />
<RadioButton
android:id="@+id/rbtn_4"
android:layout_height="match_parent"
android:layout_width="match_parent"
style="@style/style"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/bg_4"
android:tag="rbtn_4"
android:layout_weight="1.0"
android:gravity="center"
android:text="消息" />
<RadioButton
android:id="@+id/rbtn_5"
android:layout_height="match_parent"
android:layout_width="match_parent"
style="@style/style"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/bg_5"
android:tag="rbtn_5"
android:layout_weight="1.0"
android:gravity="center"
android:text="設置" />
</RadioGroup>
</LinearLayout>
</TabHost>
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends TabActivity implements OnCheckedChangeListener {
private TabHost tabHost;//定義TabHost對象
private RadioGroup radioderGroup;//定義RadioGroup對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settabHost();
}
private void settabHost() {
tabHost=this.getTabHost();//實例化TabHost
//添加選項卡並且跳轉Intent
tabHost.addTab(tabHost.newTabSpec("tab_1").setIndicator("好友")
.setContent(new Intent(this,OneActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab_2").setIndicator("廣場")
.setContent(new Intent(this,TwoActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab_3").setIndicator("首頁")
.setContent(new Intent(this,ThreeActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab_4").setIndicator("消息")
.setContent(new Intent(this,FourActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab_5").setIndicator("設置")
.setContent(new Intent(this,FiveActivity.class)));
//得到RadioGroup對象
radioderGroup=(RadioGroup) findViewById(R.id.main_radio);
//設置RadioGroup對象的切換監聽器
radioderGroup.setOnCheckedChangeListener(this);
}
//事項OnCheckedChangeListener中的RadioGroup的選項切換回調函數
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//根據所選中的RadioGroup的選項id,設置tabHost的選項卡
switch (checkedId) {
case R.id.rbtn_1:
tabHost.setCurrentTabByTag("tab_1");
break;
case R.id.rbtn_2:
tabHost.setCurrentTabByTag("tab_2");
break;
case R.id.rbtn_3:
tabHost.setCurrentTabByTag("tab_3");
break;
case R.id.rbtn_4:
tabHost.setCurrentTabByTag("tab_4");
break;
case R.id.rbtn_5:
tabHost.setCurrentTabByTag("tab_5");
break;
}
}
}
之後運行即可,可以擴展功能哦!