Android 选项卡的使用

废话不说,代码如下:


首先创建一个xml(TabHost)文件。

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
 tab1与选项卡tab1对应
<LinearLayout android:id="@+id/tab1" android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent">			<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="我是tab1"/></LinearLayout> 
 
tab2与选项卡tab2对应
<LinearLayoutandroid:id="@+id/tab2" android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="我是tab2"/></LinearLayout></TabHost>	                      



import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LayoutInflater layoutInflater = LayoutInflater.from(this);  
       
        Resources res = getResources();
        TabHost host = getTabHost();
        TabSpec spec;
        layoutInflater.inflate(R.layout.tab,host.getTabContentView(),true );
        
        
        spec=host.newTabSpec("1")
        		.setIndicator("tab1",     //选项卡的名称										
        		res.getDrawable(android.R.drawable.star_big_on))//选项卡的ICON,androdi.R 是android自带的								
               	       .setContent(R.id.tab1);//设置点击选项卡所显示的内容
         
        host.addTab(spec);      //添加选项卡
        
        spec=host.newTabSpec("2")
        		.setIndicator("tab2",      //选项卡的名称

                        res.getDrawable(android.R.drawable.star_big_on))//选项卡的ICON,androdi.R 是android自带的	

                       .setContent(R.id.tab2);           //设置点击选项卡所显示的内容

        
        host.addTab(spec);     //添加选项卡

 
        host.setCurrentTab(0);  //用来设置哪个选项卡是默认选项卡  从0开始
            
        
    }
} 


效果如图



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值