Android选项卡TabHost功能和用法

1、布局文件

复制代码
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.helloword.TabActivity" >

    <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>
复制代码

tab1.xml 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="左边 tab"/>

</LinearLayout>
复制代码

tab2.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
         android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:text="right tab"/>

</LinearLayout>
复制代码

2、Activity 

复制代码
package com.example.helloword;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;

public class TabActivity extends Activity  implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);
        
        TabHost tabHost = findViewById(android.R.id.tabhost);
        tabHost.setup();
        
        LayoutInflater inflater = LayoutInflater.from(this);
        
        inflater.inflate(R.layout.tab1, tabHost.getTabContentView());
        inflater.inflate(R.layout.tab2, tabHost.getTabContentView());
        
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("left").setContent(R.id.tab1));
        
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("right").setContent(R.id.tab2));
        
        
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        
    }

}
复制代码

 

posted @   大空白纸  阅读(2150)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2017-09-20 -bash: Chmod: command not found
2014-09-20 linq to xml 简单的增、删、改、查、保存xml文件操作
点击右上角即可分享
微信分享提示