加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_郴州站长网 (http://www.0735zz.com/)- 云通信、区块链、物联设备、云计算、站长网!
当前位置: 首页 > 教程 > 正文

Android开发教程:畅聊Tab选项卡

发布时间:2021-12-18 13:47:42 所属栏目:教程 来源:互联网
导读:Tab选项卡是一个非常方便的组件。 一.使用Tab组件的步骤: 1.在布局文件中使用FrameLayout列出Tab组件以及Tab中的内容组件 2.Activity要继承TabActivity 3.调用TabActivity的getTabHost( )方法来获得TabHost对象 4.通过TabHost创建Tab选项 二.实现不同Tab里面
Tab选项卡是一个非常方便的组件。
 
一.使用Tab组件的步骤:
 
1.在布局文件中使用FrameLayout列出Tab组件以及Tab中的内容组件
 
2.Activity要继承TabActivity
 
3.调用TabActivity的getTabHost( )方法来获得TabHost对象
 
4.通过TabHost创建Tab选项
 
二.实现不同Tab里面的内容有两种方式:
 
1.切换不同的Tab时候,不同Tab里面的内容在同一个Activity显示,主要是通过修改布局文件里面的id来实现的。下面是一个具体的例子:
 
MainActivity.java
 
package com.Android.tab.activity;  
 
import android.app.TabActivity;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.widget.TabHost;  
 
public class MainActivity extends TabActivity {  
    @Override
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
         //获得TabHost对象       
        TabHost tah = getTabHost();  
          
        // from(this)从TabActivity获取LayoutInflater  
        // R.layout.main 存放Tab布局  
        // 通过TabHost获得存放Tab标签页内容的FrameLayout  
        // 是否将inflate 加到根布局元素上  
        LayoutInflater.from(this).inflate(R.layout.main, tah.getTabContentView(), true);  
          
        //设置Tab标签的内容和显示内容  
        tah.addTab(tah.newTabSpec("tab1").setIndicator("图片1").setContent(R.id.TextView01));  
        tah.addTab(tah.newTabSpec("tab2").setIndicator("图片2").setContent(R.id.TextView02));  
        tah.addTab(tah.newTabSpec("tab3").setIndicator("图片3").setContent(R.id.TextView03));                        
    }  
}
main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout01"   
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"
    >     
    <TabHost   
        android:id="@+id/TabHost01"
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        />
    <TextView   
        android:id="@+id/TextView01"   
        android:background="@drawable/pic1"
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        />    
    <TextView   
        android:id="@+id/TextView02"   
        android:background="@drawable/pic2"
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        />    
    <TextView   
        android:id="@+id/TextView03"   
        android:background="@drawable/pic3"
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        />    
</FrameLayout>
 

(编辑:开发网_郴州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读