android TabHost制作底部导航栏

我做的例子是底部导航栏,底部四个导航,点击不同的导航切换不同的画面。如图所示:

首先主页面是一个<TabHost></TabHost>且它的id为@android:id/tabhost,里面必须包含一个<FrameLayout></FrameLayout>且id为@android:id/tabcontent,然后布局里有个<TabWidget></TabWidget>且id为@android:id/tabs。具体如下:

<?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">
        <!-- 切换的tab画面 -->
        <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="50dip"
   android:background="@drawable/border_style"
   >
    <RadioGroup 
       android:layout_gravity="bottom"
       android:orientation="horizontal"
       android:gravity="center_vertical"
       android:id="@+id/radiomenu"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content">
       
       <RadioButton android:id="@+id/rb1"
                android:text="@string/shouye"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/icon_1_n"
                style="@style/bottom_button"/>
       <RadioButton android:id="@+id/rb2"
                android:text="@string/dingdan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/icon_2_n"
                style="@style/bottom_button"/>
       <RadioButton android:id="@+id/rb3"
                android:text="@string/shangpin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/icon_3_n"
                style="@style/bottom_button"/>
       <RadioButton android:id="@+id/rb4"
                android:text="@string/wode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/icon_4_n"
                style="@style/bottom_button"/>
    </RadioGroup>
</TabWidget>
    </LinearLayout>
</TabHost>

主画面MainActivity要继承ActivityGroup,实现OnCheckedChangeListener接口,来判断点击了哪个tab做对应的控制。

package com.example.tabhostdemo;


import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;


public class MainActivity extends ActivityGroup implements OnCheckedChangeListener{


private TabHost host;
private Intent aIntent;
private Intent bIntent;
private Intent cIntent;
private Intent dIntent;
private RadioButton button1;
private RadioButton button2;
private RadioButton button3;
private RadioButton button4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aIntent = new Intent(this, TabOneActivity.class);
bIntent = new Intent(this, TabTwoActivity.class);
cIntent = new Intent(this, TabThreeActivity.class);
dIntent = new Intent(this, TabFourActivity.class);
button1 = (RadioButton)findViewById(R.id.rb1);
button2 = (RadioButton)findViewById(R.id.rb2);
button3 = (RadioButton)findViewById(R.id.rb3);
button4 = (RadioButton)findViewById(R.id.rb4);
host = (TabHost)findViewById(android.R.id.tabhost);
host.setup();
host.setup(this.getLocalActivityManager());
host.addTab(host.newTabSpec("tab1").setContent(aIntent).setIndicator("tabOne"));
host.addTab(host.newTabSpec("tab2").setContent(bIntent).setIndicator("tabTwo"));
host.addTab(host.newTabSpec("tab3").setContent(cIntent).setIndicator("tabThree"));
host.addTab(host.newTabSpec("tab4").setContent(dIntent).setIndicator("tabFour"));
button1.setOnCheckedChangeListener(this);
button2.setOnCheckedChangeListener(this);
button3.setOnCheckedChangeListener(this);
button4.setOnCheckedChangeListener(this);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
switch (buttonView.getId()) {
case R.id.rb1:
this.host.setCurrentTabByTag("tab1");
break;
case R.id.rb2:
this.host.setCurrentTabByTag("tab2");
break;
case R.id.rb3:
this.host.setCurrentTabByTag("tab3");
break;
case R.id.rb4:
this.host.setCurrentTabByTag("tab4");
break;
default:
break;
}
}
}
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值