Android选项卡TabHost组件的使用

选项卡TabHost的介绍:

选项卡用于实现一个多标签页的用户界面,通过它可以将一个复杂的对话框分割成若干个标签页,实现对信息的分类显示和管理。使用该组件不仅可以使界面简洁大方,还可以有效地减少窗体的个数。例如,微信的表情商店界面:

TabHost的实现分为两种,一个是不继承TabActivity,一个是继承自TabActivity;

方法一:不用继承TabActivity,在布局文件中使用TabHost组件,使用findViewById获取TabHost组件:
① 在界面布局中定义TabHost组件,并为该组件定义选项卡内容;
② 使用findViewById获取TabHost组件;
③ 通过TabHost对象的方法来创建和添加选项卡子布局文件tab1.xml,tab2.xml;

         newTabSpec(String tag):    //创建选项卡
         addTab(TabHost.TabSpec tabSpec): //添加选项卡

主类TabHostActivity.java

package com.xw.tabhost;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class TabHostActivity extends AppCompatActivity {

   private TabHost tabHost; //声明TabHost组件的对象
   @Override
   protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_tab_host);
         //获取布局文件R.layout.activity_tab_host中TabHost组件对象
         tabHost=(TabHost)findViewById(android.R.id.tabhost);
         //初始化TabHost组件
         tabHost.setup();
         //声明并实例化一个LayoutInflater对象
         LayoutInflater inflater = LayoutInflater.from(this);
         inflater.inflate(R.layout.tab1, tabHost.getTabContentView());
         inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
     /**
      * TabHost是一个简单的容器,提供如下两种方法来创建选项卡
      * newTabSpec(String tag):创建选项卡
      * addTab(TabHost.TabSpec tabSpec):添加选项卡
      */
     //在TabHost组件中创建添加第一个标签页,然后设置:标题/图标/标签页布局  
     tabHost.addTab(tabHost.newTabSpec("tab1")
             .setIndicator("精选表情").setContent(R.id.linearlayout1));
     //在TabHost组件中创建添加第二个标签页,然后设置:标题/图标/标签页布局
     tabHost.addTab(tabHost.newTabSpec("tab2")
             .setIndicator("投稿表情").setContent(R.id.framelayout)) ;
   }
}

在主布局文件activity_tab_host.xml中,添加实现选项卡所需的 TabHost、TabWidget FrameLayout 组件。具体的步 骤是:首先添加一个 TabHost 组件,然后在该组件中添加线性布局管理器,并且在该布局管理器中添加一个作为标签组的 TabWidget 和一个作为标签内容的 FrameLayout 组件,最后删除内边距。 如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@android:id/tabhost"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".TabHostActivity">
     <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
        
         <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">
        </FrameLayout>
     
     </LinearLayout>
</TabHost>

在 res/layout 目录下单击右键 New Layout resource file 创建两个 XML 布局文件,名称为 tab1.xml、tab2.xml,用于指定左右两个标签页中要显示的内容,这里只展示一个ImageView,具体代码如下:

tab1.xml文件如下:

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

tab2.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/framelayout"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
       <LinearLayout
           android:id="@+id/linearlayout2"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
          <ImageView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:src="@mipmap/biaoqing_right"/>
       </LinearLayout>
</FrameLayout>

运行效果见上图!

方法二:继承TabActivity,在布局文件中不使用TabHost组件:
主布局文件不需要定义TabHost组件,这里用三个垂直的LinearLayout作为标签页,每个标签页里面有一个TextView组件,main_tab.xml代码如下:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabcontent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- 定义第一个标签页的内容 -->
    <LinearLayout
        android:id="@+id/tab01"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个标签页的TextView组件"
            android:textSize="8pt" />

    </LinearLayout>
    <!-- 定义第二个标签页的内容 -->
    <LinearLayout
        android:id="@+id/tab02"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个标签页的TextView组件"
            android:textSize="8pt" />
    </LinearLayout>

    <!-- 定义第三个标签页的内容 -->
    <LinearLayout
        android:id="@+id/tab03"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第三个标签页的TextView组件"
            android:textSize="8pt" />
    </LinearLayout>
</FrameLayout>

在主类MainTabActivity中继承TabActivity类:

package com.xw.tabhost2;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.Toast;

public class MainTabActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main_tab);
        // 获取该Activity里面的TabHost组件
        TabHost tabHost = getTabHost();
        LayoutInflater.from(this).inflate(R.layout.main_tab,
                tabHost.getTabContentView(), true);

        /* 以上创建和添加标签页也可以用如下代码实现 */
        tabHost.addTab(tabHost.newTabSpec("tab1")
               .setIndicator("标签页一").setContent(R.id.tab01));
        tabHost.addTab(tabHost.newTabSpec("tab2")
               .setIndicator("标签页二").setContent(R.id.tab02));
        tabHost.addTab(tabHost.newTabSpec("tab3")
               .setIndicator("标签页三").setContent(R.id.tab03));

        //标签切换事件处理,setOnTabChangedListener
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
            @Override
            // tabId是newTabSpec参数设置的tab页名,并不是layout里面的标识符id
            public void onTabChanged(String tabId) {
                if (tabId.equals("tab1")) {   //第一个标签
                    Toast.makeText(MainTabActivity.this, "点击标签页一", 
                                                  Toast.LENGTH_SHORT).show();
                }
                if (tabId.equals("tab2")) {   //第二个标签
                    Toast.makeText(MainTabActivity.this, "点击标签页二", 
                                                   Toast.LENGTH_SHORT).show();
                }
                if (tabId.equals("tab3")) {   //第三个标签
                    Toast.makeText(MainTabActivity.this, "点击标签页三", 
                                                    Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}


注意:这里第一没有使用setContentView,而是用inflate方法;第二在设置标签页改变监听器时,onTabChanged的参数tabId是使用newTabSpec方法的参数设置的标签页名称,而不是布局文件中标签页的标识符Id;

运行结果:

发布了9 篇原创文章 · 获赞 0 · 访问量 1428

猜你喜欢

转载自blog.csdn.net/luqingshuai_eloong/article/details/104253159