android之TabWidget选项卡

 /**
 * Tab选项卡类似与电话本的界面,通过多个标签切换不同的内容,要实现这个效果,首先要知道TabHost,
 * 它是一个用来存放多个Tab标签的容器,每一个Tab都可以对应自己的布局,比如,电话本中的Tab布局就
 * 是一个线性布局
 * 要使用TabHost,首先要通过getTabHost方法获取TabHost的对象,然后通过addTab方法来向
 * TabHost中添加Tab,当然每个Tab在切换时都会产生一个事件,要捕捉这个事件,需要设置TabActivity
 * 的事件监听setOnTabChangedListener
 */
 
效果图


 发现很多微薄如腾讯,新浪的选项卡 都是显示在页面底部的,网上有资料:通过反射获取tabwidget中的私有变量,改变其值。
   <!-- 实现Tab标签的居底主要是通过设置属性 android:layout_weight="1" -->
  <!-- 还要注意FrameLayout标签的位置,要写在TabWidget标签的前面 -->
  效果图
  
 
   本程序main.xml源码 
[html] view plaincopy
<?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">  
  <!-- 实现Tab标签的居底主要是通过设置属性 android:layout_weight="1" -->  
  <!-- 还要注意FrameLayout标签的位置,要写在TabWidget标签的前面 -->  
  <FrameLayout  
   android:id="@android:id/tabcontent"  
   android:layout_weight="1"   
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent" />  
  
  <TabWidget   
   android:id="@android:id/tabs"  
   android:layout_alignParentBottom="true"   
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content" />  
 </LinearLayout>  
</TabHost>  

 
java源码
[html] view plaincopy
import android.app.TabActivity;        
import android.os.Bundle;     
import android.widget.TabHost;     
import android.widget.Toast;     
import android.widget.TabHost.OnTabChangeListener;  
  
public class TabWidgetActivity extends TabActivity   
{  
    TabHost tabhost;     
    @Override    
    public void onCreate(Bundle savedInstanceState)  
    {     
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);     
          
        //取得TabHost对象     
          tabhost = getTabHost();     
          
        //为TabHost添加标签     
         //新建一个newTabSpec(newTabSpec)     
        //设置其标签和图标(setIndicator)     
         //设置内容(setContent)     
          tabhost.addTab(tabhost.newTabSpec("tab1")     
                .setIndicator("TAB 1",getResources().getDrawable(R.drawable.icon))    
                .setContent(R.id.text1));     
         tabhost.addTab(tabhost.newTabSpec("tab2")     
                .setIndicator("TAB 2",getResources().getDrawable(R.drawable.icon))    
                .setContent(R.id.text2));     
         tabhost.addTab(tabhost.newTabSpec("tab3")     
                .setIndicator("TAB 3",getResources().getDrawable(R.drawable.icon))    
                .setContent(R.id.text3));     
         //设置TabHost的背景颜色     
           //tabhost.setBackgroundColor(Color.argb(150,22,70,150));     
         //设置TabHost的背景图片资源     
           tabhost.setBackgroundResource(R.drawable.bg0);     
        //设置当前显示哪个标签     
           tabhost.setCurrentTab(0);     
          
        //标签切换事件处理,setOnTabChangedListener     
        tabhost.setOnTabChangedListener(new OnTabChangeListener()     
        {     
            public void onTabChanged(String tabId)     
            {               
                switch(tabhost.getCurrentTab())  
                {  
                case 0:  
                          tabhost.setBackgroundResource(R.drawable.bg0);  
                         Toast.makeText(getApplicationContext(), "当前标签为第一个页面", Toast.LENGTH_SHORT).show();  
                         break;  
                 case 1:  
                         tabhost.setBackgroundResource(R.drawable.bg1);  
                         Toast.makeText(getApplicationContext(), "当前标签为第二个页面", Toast.LENGTH_SHORT).show();  
                         break;  
                case 2:  
                         tabhost.setBackgroundResource(R.drawable.bg2);  
                         Toast.makeText(getApplicationContext(), "当前标签为第三个页面", Toast.LENGTH_SHORT).show();  
                         break;  
                }                              
            }     
        });      
    }     
}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值