Tabhost的简单用法

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"    这里必须使用系统的id/tabhost
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TabWidget
            android:id="@android:id/tabs"   这里必须使用系统的id/tabs
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
          />

        <FrameLayout         这里必须使用系统的id/tabcontent
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@android:id/tabs" />
    </RelativeLayout>
</TabHost>

之所以引入Relative 布局是为了方便Tabwidget 控件和Framelayout之间的布局方便

java类列的

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_main_layout);
        
        mtabhost = getTabHost();//实例化Tabhost

    
        layoutInflater = LayoutInflater.from(this);
        
        TabHost.TabSpec tabSpec1 = mtabhost.newTabSpec("tab1");    //添加TabSpec 由mtabhost.newTabSpec()得到,而tab1是叶卡项
        tabSpec1.setIndicator(indicatorview(TAB_LIST));//可以设定文字或者view
        tabSpec1.setContent(new Intent(this, Mt_Activity.class));//实现的界面

        TabHost.TabSpec tabSpec2 = mtabhost.newTabSpec("tab2");
        tabSpec2.setIndicator(indicatorview(TAB_GRID));
        tabSpec2.setContent(new Intent(this, ToolBar_Activity.class));

        TabHost.TabSpec tabSpec3 = mtabhost.newTabSpec("tab3");
        tabSpec3.setIndicator(indicatorview(TAB_PROGRESS_BAR));
        tabSpec3.setContent(new Intent(this, Progress_Dialog_Activity.class));

        mtabhost.addTab(tabSpec1);//添加
        mtabhost.addTab(tabSpec2);
        mtabhost.addTab(tabSpec3);

      

        public View  indicatorview(int pageNo){

        View view1 = layoutInflater.inflate(R.layout.activity_tab_demo1,null);//解析tab的布局
        ImageView iconimg = (ImageView) view1.findViewById(R.id.indicator_imageview);
        TextView  textView = (TextView) view1.findViewById(R.id.indicator_textview);
        switch (pageNo){
            case 0:
                iconimg.setImageResource(R.drawable.ic_action_search);//重新设定tab项的布局
                textView.setText("首页");
                break;
            case 1:
                iconimg.setImageResource(R.drawable.ic_action_back);
                textView.setText("团购");
                break;
            case 2:
                iconimg.setImageResource(R.drawable.ic_action_home);
                textView.setText("设置");
                break;
        }
        return view1;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值