Android TabHost

时间:2020-01-09
本文章向大家介绍Android TabHost ,主要包括Android TabHost 使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

学习自

Overview

TabHost早就已经被Google弃用了。但是它弃归它弃我用归我用。但是Google也推出了替代品。

  • FragmentTabHost
  • TabLayout(Material Design)

如何优雅的使用TabHost

按照国际惯例,我们先看一下代码<布局>:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/navTH"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <!--TabWidget的名字是固定的,不可以乱改-->
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <!--FrameLayout的名字是要求的,不可以乱改-->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <RelativeLayout
                    android:id="@+id/tab1RLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="Tab Host 1" />
                </RelativeLayout>


                <RelativeLayout
                    android:id="@+id/tab2RLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="Tab Host 1" />
                </RelativeLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

这样我们的Android 使用 TabHost 的布局文件就完成了。
我们的布局文件需要注意以下几点,第一个就是我们几个必须的控件是必须要的。
还需要注意的是:我们的android的主要控件的id是不可以进行自己起名字的。
当我们的布局文件完成以后我们需要进行TabHost的核心代码开发。

Android TabHost的核心代码开发

先上代码:
package com.android.gesture.style_tabhost;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private TabHost mNavTH;

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

}

void initFindView() {
    mNavTH = this.findViewById(R.id.navTH);
}

void init() {
    mNavTH.setup();

    mNavTH.addTab(mNavTH.newTabSpec("Tab1").setIndicator("New").setContent(R.id.tab1RLayout));
    mNavTH.addTab(mNavTH.newTabSpec("Tab2").setIndicator("Video").setContent(R.id.tab2RLayout));

    mNavTH.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
            Toast.makeText(MainActivity.this, tabId, Toast.LENGTH_SHORT).show();
        }
    });
}

}
然后我们根据我们的源码看一下这边的操作。

  • 先将我们的TabHost控件实例化
  • 然后就是添加Tab到TabHost中
  • 先是需要将调用.setup();的方法
  • 然后将你需要添加的Tab添加到TabHost中

这样的话,我们的TabHost开发就完成了。十分的简单。

但是!!

你们没有方向我们的上面这个方法将所有的界面布局全部添加在Layout资源文件夹中了么!?这个是非常不友好的!


如何优雅的写TabHost

我们先看一下我们主活动的布局代码:

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

    <TabHost
        android:id="@+id/navTH"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <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"/>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>
    </TabHost>
</LinearLayout>

这里我只创建了最最基本的TabHost的方法。

然后就是我们创建你所需要选项卡先是的活动,注意是活动不仅仅是布局文件。
这样我们对比在布局文件中添加好很多,直接添加活动,可以让你的文件管理更加的详细清晰,而且在添加逻辑代码的时候不会太乱。

说了这么多,我们看一下我们的核心代码把。

package com.android.gesture.style_tabhost;

import androidx.appcompat.app.AppCompatActivity;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;

import java.io.InterruptedIOException;

public class MainActivity extends AppCompatActivity {
    private TabHost mNavTH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNavTH = findViewById(R.id.navTH);
        init(savedInstanceState);
    }

    void init(Bundle saveInstanceState) {
        LocalActivityManager localActivityManager = new LocalActivityManager(this, true);
        localActivityManager.dispatchCreate(saveInstanceState);

        mNavTH.setup(localActivityManager);

        mNavTH.addTab(mNavTH.newTabSpec("Tab1").setIndicator("New").setContent(new Intent(this, TempActivity.class)));
        mNavTH.addTab(mNavTH.newTabSpec("Tab1").setIndicator("New").setContent(new Intent(this, Temp2Activity.class)));
    }
}

这些就是我们添加的Tab到TabHost的全部过程了。
可以看到我么这里直接使用了活动添加而不是layout添加,很好用哦。

原文地址:https://www.cnblogs.com/cao-1/p/12172321.html