选项卡(TabWidget) 使用例子

1.action 必须继承tabActivity,

2.配置xml 文件,TabHost ,TabWidget ,FrameLayout的ID必须分别为@android:id/tabhost,@android:id/tabs,@android:id/tabcontent,同时LinearLayout的android:orientation=”vertical”(LinearLayout默认是横向的)

tab.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content"></TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />

    </LinearLayout>


package com.maoyang.layout;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class TabWidget extends TabActivity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);
        //系统资源文件类
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabSpec spec;
        Intent intent;  // Reusable Intent for each tab
     
        //第一个TAB
        intent = new Intent(this,OneTabActivity.class);//新建一个Intent用作Tab1显示的内容
        spec = tabHost.newTabSpec("tab1")//新建一个 Tab
        .setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_menu_manage))//设置名称以及图标
        .setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx
        tabHost.addTab(spec);//添加进tabHost
 
        //第二个TAB
        intent = new Intent(this,TwoTabActivity.class);//第二个Intent用作Tab1显示的内容
        spec = tabHost.newTabSpec("tab2")//新建一个 Tab
        .setIndicator("Tab2", res.getDrawable(android.R.drawable.ic_dialog_map))//设置名称以及图标
        .setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx
        tabHost.addTab(spec);//添加进tabHost
        
        //tabHost.setBackgroundColor(Color.argb(150,22,70,150));
        //设置TabHost的背景图片资源
        tabHost.setBackgroundResource(R.drawable.desktop);
        tabHost.setOnTabChangedListener(tabChangeListener);
        tabHost.setCurrentTab(0);
    }
    
    private OnTabChangeListener tabChangeListener=new OnTabChangeListener()     
    {     
        public void onTabChanged(String tabId)     
        {     
            Toast toast=Toast.makeText(getApplicationContext(), "现在是"+tabId+"标签", Toast.LENGTH_SHORT);     
            toast.show();     
        }     
    };
    
}

</TabHost>//注释,TabHost 由多个TabSpec 组成,tabHost.newTabSpec("tab1") 这里的tab1 表示TabSpec 的id


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值