AS之TabHost

1.概述:TabHost(选项卡组件),可以在一个窗口中显示多个标签栏的内容。主要功能是进行应用程序的分类管理,比如手机的通话记录,未接来电,已接电话。

2.TabHost(选项卡)构成

  • TabHost:根布局
  • 选项卡整体布局:可设置标签栏和标签页内容的布局位置
  • 标签栏(TabWidget):用于实现一个多标签的用户见面,通过将一个复杂的对话框分割成若干个标签页,实现对信息的分类显示和管理
  • 标签页内容(FrameLayout):以层的方式,放置每一个标签页显示的内容
    在这里插入图片描述
    3.TabHost类的常用方法
方法作用
newTabSpec(String tag)创建一个标签页
addTab (TabHost.TabSpectabSpec)添加标签页
getCurrentView()获取当前的View组件
setup()初始化TabHost组件
setCurrentTab(int index)设置当前标签页的编号
setCurrentTabByTag(String tag)设置当前标签页的编号
getTabContentView获取标签页内容视图对象,即FrameLayout对象
setOnTabChangeListener(TabHost.OnTabChangeListener))注册选项卡中标签改变时的事件监听器

4.TabWidget类(标签栏)

方法作用
TabWidget(Context context)创建TabWidget对象
addView(View child)向TabWidget添加组件
getTabCount()获取标签个数
setEnabled(boolean enabled)设置是否启用

5.实现选项卡的一般步骤
1) (主布局文件)
首先使用TabHost作为根布局管理器,再设置选项卡的整体布局。在选项卡的整体布局管理器中,再添加标签栏TabWidget和选项卡中标签页内容FrameLayout两个组件

2) (选项卡的标签页内容)

  • 方式1:独立创建各个标签页的XML文件
  • 方式2:在布局文件中的TabH的FrameLayout组件中,直接设置各个标签页的布局内容(需要定义ID来区别)

3) (控制TabHost组件)
在Activity的onCreate()中,获取并初始化TabHost组件,然后创建设置标签页,最后为TabHost组件添加标签页

方法1:

  • LayoutInflater(布局填充器)

  • 方法1不是通过< TabHost >元素的< FrameLayout> 布局管理器中定义组件的形式,此时需要通过LayoutInflater(布局填充器)来实现标签页内容的填充

  • 常用方法

方法作用
public static LayoutInflater from(Context context)从指定的上下文中获取LayoutInflater(布局填充器)对象
public View inflater(int resource,ViewGroup root)按照指定的布局管理器资源ID,向选中标签内容对象填充内容,即向FrameLayout对象填充内容

-案例

activity-main

<?xml version="1.0" encoding="utf-8"?>
<TabHost           //-选项卡
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabhost">
    <LinearLayout    //选项卡整体布局
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TabWidget   //-标签栏
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"/>
        <FrameLayout  //标签页内容
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent"/>
    </LinearLayout>

</TabHost>

tab1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearlayout1"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="宁静致远"
        android:layout_marginTop="20dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="雪儿"
        android:layout_marginTop="20dp"/>

</LinearLayout>

tab2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearlayout2"
    android:orientation="vertical"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小草"
        android:drawableLeft="@mipmap/ic_launcher"
        android:drawablePadding="20dp"
        android:paddingTop="20dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小花"
        android:drawableLeft="@mipmap/ic_launcher_round"
        android:drawablePadding="20dp"
        android:paddingTop="20dp"/>

</LinearLayout>

mainActivity

package com.example.a5_5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class MainActivity extends AppCompatActivity {
    private TabHost tabHost;   //1.定义TabHost对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 找到TabHost选项卡组件并初始化
        tabHost=(TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        // 创建LayoutInflater(布局填充器)
        LayoutInflater inflater=LayoutInflater.from(this);
        //将标签布局文件资源填充到选项卡的FrameLayout对象中
              inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
        inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
        //4. 向选项卡中添加标签页
        tabHost.addTab(tabHost.newTabSpec("tag1").setIndicator("未接来电").setContent(R.id.linearlayout1));
        tabHost.addTab(tabHost.newTabSpec("tag2").setIndicator("已接来电").setContent(R.id.linearlayout2));
    }
}

方法二

  • TabHost.TabSpec类(标签页)
    TabHost.TabSpec类是TabHost的内部类,,是标签页类。需要通过TableHost类的newTabSpec(String tag)方法创建标签页对象
  • 常用方法
方法作用
public TabHost.TabSpec.setIndicator(CharSpeqence label设置标签页的标签
public TabHos.TabSpec setContent(int viewid)按照组件ID的内容设置标签页的内容
  • 例子
    layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost          //--选项卡
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabhost">
    <RelativeLayout         //--整体布局
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TabWidget               //--标签
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"
            android:layout_alignParentBottom="true"/>
        <FrameLayout        //--页面布局
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent">
            <LinearLayout  //--编辑框
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/tab_edit"
                android:layout_margin="10dp"
                android:orientation="vertical">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入搜索内容"
                    android:id="@+id/edit"
                    android:textSize="18px"/>
                <Button
                    android:id="@+id/but"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="搜索"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/tab_clock"
                android:orientation="vertical">
                <AnalogClock
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/analogclock"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/radio"
                android:orientation="vertical">
                <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/radiogroup"
                    android:orientation="vertical">
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="男"
                        android:id="@+id/male"/>
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:id="@+id/female"/>
                </RadioGroup>
            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>
</TabHost>

package com.example.a5_6;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    private TabHost tabHost;       //--定义自变量
    private int[] arrayList=new int[]{R.id.edit,R.id.analogclock,R.id.radiogroup};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //-找到组件,初始化
        tabHost=(TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        for (int i=0;i<arrayList.length;i++){       //--建立循环
            TabHost.TabSpec mytab=tabHost.newTabSpec("tag"+i);//--新建标签
            mytab.setIndicator("标签-"+i);//标签标记
            mytab.setContent(arrayList[i]);  //为标签添加内容
            tabHost.addTab(mytab);//将标签添加到选项卡
        }
        tabHost.setCurrentTab(0);   //默认显示第0个
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHostAndroid 中的一个容器,用于实现多个标签页之间的切换。下面是 TabHost 的使用方法详解: 1. 在 XML 布局文件中添加 TabHost 控件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <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="0dp" android:layout_weight="1" /> </LinearLayout> </TabHost> ``` 2. 在 Activity 中初始化 TabHost: ```java TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 添加标签页: ```java TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Tab1"); tabSpec1.setIndicator("Tab 1"); tabSpec1.setContent(R.id.tab1_content); TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Tab2"); tabSpec2.setIndicator("Tab 2"); tabSpec2.setContent(R.id.tab2_content); tabHost.addTab(tabSpec1); tabHost.addTab(tabSpec2); ``` 4. 创建标签页的布局: 在布局文件中创建用于显示每个标签页内容的 View,例如: ```xml <LinearLayout android:id="@+id/tab1_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 1 的内容 --> </LinearLayout> <LinearLayout android:id="@+id/tab2_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 2 的内容 --> </LinearLayout> ``` 以上就是 TabHost 的基本使用方法。通过添加不同的标签页和对应的布局,可以实现多个标签页之间的切换和显示不同的内容。你可以根据自己的需求来定制标签页的样式和内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值