zoukankan      html  css  js  c++  java
  • Android TabHost使用

    TabHost是Android中自带的选项卡控件,效果图如下:

    主布局文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <TabHost
            android:id="@+id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
    
            <LinearLayout
                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" >
                </TabWidget>
    
                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
    
                    <LinearLayout
                        android:id="@+id/tab1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >
                    </LinearLayout>
    
                    <LinearLayout
                        android:id="@+id/tab2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" 
                        android:orientation="vertical">
                    </LinearLayout>
    
                    <LinearLayout
                        android:id="@+id/tab3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    
    </RelativeLayout>

    核心代码:

    package com.tabhost;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ActivityGroup;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.Window;
    import android.widget.TabHost;
    
    public class MainActivity extends ActivityGroup {
    
    
        private TabHost mTabHost;
         
          protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            // 设置TabHost
            initTabs();
          }
         
          private void initTabs() {
            mTabHost = (TabHost) findViewById(R.id.tabhost);
            mTabHost.setup(this.getLocalActivityManager());
            // 添加日志列表的tab,注意下面的setContent中的代码.是这个需求实现的关键
            mTabHost.addTab(mTabHost.newTabSpec("tab_log")
              .setIndicator("日志",getResources().getDrawable(R.drawable.login_bg))
              .setContent(new Intent(this, LogActivity.class)));
         
            // 添加应用设置的tab,注意下面的setContent中的代码.是这个需求实现的关键
            mTabHost.addTab(mTabHost.newTabSpec("tab_setting")
              .setIndicator("设置",getResources().getDrawable(R.drawable.ic_launcher))
              .setContent(new Intent(this, SettingActivity.class)));
            mTabHost.setCurrentTab(1);
          }
        
    }

    两个子页面很简单,两个Activity以及对应布局。

  • 相关阅读:
    编译Linux 2.6内核
    C语言学习参考(基础&进阶)
    用户体验为什么如此重要
    北京讲座:软件企业常见问题和系统性解决方法(7月5日)
    《JavaScript权威指南(第6版)》诚征广大读者参与初译稿审校活动!
    有关正则表达式的研究
    关于分页的研究
    四舍五入等一些不常用代码整理
    最简单CSS实现Table细线表格
    vs2005 SP1补丁安装慢
  • 原文地址:https://www.cnblogs.com/hyyweb/p/5359585.html
Copyright © 2011-2022 走看看