Android开发之TabHost

TabHost:


在XML中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/mainback">

    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"/>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </FrameLayout>
        </RelativeLayout>
    </TabHost>

</LinearLayout>

然后创建三个布局文件tab1,tab2,tab3,里面的内容分别对应三个选显卡的内容。
 
最后在class:
 
 
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class MainActivity extends Activity {
    TabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();
        //加载选项卡1,2,3对应的布局
        LayoutInflater layoutInflater=LayoutInflater.from(this);
        layoutInflater.inflate(R.layout.tab1,tabHost.getTabContentView());  // getTabContentView():Get the FrameLayout which holds tab content
        layoutInflater.inflate(R.layout.tab2,tabHost.getTabContentView());
        layoutInflater.inflate(R.layout.tab3,tabHost.getTabContentView());
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("标签页一")
                .setContent(R.id.line1));
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("标签页二")
                .setContent(R.id.line2));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("标签页三")
                .setContent(R.id.line3));
    }
}

 
如果不想另外创建布局文件作为选项卡的内容:可以在FrameLayout中写写选项卡内容
	
常用的方法:
 

public int getCurrentTab ();             //获取当前Tab的index

public String getCurrentTabTag ();          //获取当前Tab的tag

public View getCurrentTabView ();          //获取当前Tab的视图

public void setCurrentTab (int index);         //设置当前显示哪个Tab

public void setCurrentTabByTag (String tag);    //设置当前显示哪个Tab

 

         mTabHost.addTab(mTabHost.newTabSpec("home")
                 .setIndicator("主页", getResources().getDrawable(R.drawable.home))//设置Tab标签和图标
                 .setContent(R.id.homeimage));        //设置Tab内容



利用LayoutInflator自定义选项卡按钮的样式:

private void setTabHost(){
    TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
    tabHost.setup();

    LayoutInflater li=LayoutInflater.from(this);
    li.inflate(R.layout.tab1,tabHost.getTabContentView());
    li.inflate(R.layout.tab2,tabHost.getTabContentView());
    li.inflate(R.layout.tab3,tabHost.getTabContentView());


    tabHost.addTab(tabHost.newTabSpec("tab1")
            .setIndicator(getTabItem("首页",R.drawable.ico_home))
            .setContent(R.id.homeTab));
    tabHost.addTab(tabHost.newTabSpec("tab2")
            .setIndicator(getTabItem("课表",R.drawable.pic_class))
            .setContent(R.id.classTab));
    tabHost.addTab(tabHost.newTabSpec("tab3")
            .setIndicator(getTabItem("我的",R.drawable.pic_mine))
            .setContent(R.id.groupTab));	//groupTab是要显示内容布局的id
}

private View getTabItem( String name, int image){
    LinearLayout tabLayout=(LinearLayout)LayoutInflater.from(this).inflate(R.layout.tab_item,null);
    ImageView iv=(ImageView)tabLayout.findViewById(R.id.imageView);
    TextView tv=(TextView)tabLayout.findViewById(R.id.textView);
    iv.setBackgroundResource(image);
    tv.setText(name);
    return tabLayout;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值