TabHost的两种实现方式

TabHost在App中用的还是比较多的,因为最近使用TabHost的时候出现了些小插曲,特写此博文;一来是为了下次使用到时候能够印象深刻,二来是为了帮助和我一样出现过类似错误的朋友。

下面直接进入正题

第一种:使用系统给定的id,并且继承自TabActivity,这个也是最简单的。

下面贴上布局代码

<?xml version="1.0" encoding="utf-8"?>
    <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="com.xuyongjun.application.MainActivity">

        <!--第一种:使用系统给定的Id-->
        <TabHost
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabhost">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:id="@android:id/tabcontent"></FrameLayout>

                <TabWidget
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@android:id/tabs">
                </TabWidget>
            </LinearLayout>
        </TabHost>
    </RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

下面贴上Java代码。注意,本人使用的是Activity作为Content

    public class SystemTabActivity extends TabActivity {

        private String[] mTag = {"tag1","tag2","tag3","tag4"};
        private Intent[] mIntent;
        private String[] mIndicator = {"驾考","学车","买车","百宝箱"};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            /*
            初始化Intent
             */
            initIntent();

            /*
            设置TabHost
             */
            initTabHost();
        }

        private void initIntent() {
            Intent intent1 = new Intent(this,A_Activity.class);
            Intent intent2 = new Intent(this,B_Activity.class);
            Intent intent3 = new Intent(this,C_Acitivity.class);
            Intent intent4 = new Intent(this,D_Activity.class);
            mIntent = new Intent[]{intent1,intent2,intent3,intent4};
        }

        private void initTabHost() {
            TabHost tabHost = getTabHost();

            for (int i = 0; i < mIntent.length; i++) {
                tabHost.addTab(
                                    tabHost.newTabSpec(mTag[i]).setIndicator(mIndicator[i]).setContent(mIntent[i]));
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

就此,第一种方式编写完成;运行后看看效果。

TabHost


第二种:使用自己定义id,就不用继承TabActivity了;我就是在这种方法上犯了些小错误,哈哈这个才是重点。

既然是使用自己定义的Id,那么我们只要改下布局TabHost的Id的就可以,和上面布局代码一致,这里就不全贴出来了。

<!--第二种:使用自己定义Id-->
    <TabHost
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabhost">
  • 1
  • 2
  • 3
  • 4
  • 5

我们知道TabActivity是继承自ActivityGroup,可以管理多个Activity;如果自定Id的话我们就需要自己调用API来管理这多个Activity,就需要用到LocalActivityManager。下面贴出Java代码:

public class CustomTabActivity extends Activity {

        private TabHost mTabHost;

        private String[] mTag = {"tag1","tag2","tag3","tag4"};
        private Intent[] mIntent;
        private String[] mIndicator = {"驾考","学车","买车","百宝箱"};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            /*
            初始化Intent
             */
            initIntent();

            /*
            设置TabHost
             */
            initTabHost(savedInstanceState);
        }

        private void initIntent() {
            Intent intent1 = new Intent(this,A_Activity.class);
            Intent intent2 = new Intent(this,B_Activity.class);
            Intent intent3 = new Intent(this,C_Acitivity.class);
            Intent intent4 = new Intent(this,D_Activity.class);
            mIntent = new Intent[]{intent1,intent2,intent3,intent4};
        }

        private void initTabHost(Bundle savedInstanceState) {
            mTabHost = (TabHost) findViewById(R.id.tabhost);

             /*
            辅助类来管理多个嵌入式Activity在同一进程中运行
             */
            LocalActivityManager manager = new LocalActivityManager(this,false);
            /*
            用于恢复Activity的状态,这里传入Activity onCreate()中的Bundle就行了。
             */
            manager.dispatchCreate(savedInstanceState);

            /*
            设置给TabHost就可以了
             */
            mTabHost.setup(manager);

            for (int i = 0; i < mIntent.length; i++) {
                mTabHost.addTab(
                        mTabHost.newTabSpec(mTag[i]).setIndicator(mIndicator[i]).setContent(mIntent[i]));
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

运行效果和上图一致,就不贴出来了,可能文中有些东西我理解的不太透彻,还希望各位多多指教。

猜你喜欢

转载自blog.csdn.net/jun_tong/article/details/80768760