Android控件开发之TabWidget

来源:互联网 发布:php实现导出excel 编辑:程序博客网 时间:2024/05/18 02:46

 /**
 * 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
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:id="@android:id/tabhost" android:layout_width="fill_parent"  
  4.    android:layout_height="fill_parent">  
  5.  <LinearLayout   
  6.    android:orientation="vertical"  
  7.    android:layout_width="fill_parent"   
  8.    android:layout_height="fill_parent">  
  9.   <!-- 实现Tab标签的居底主要是通过设置属性 android:layout_weight="1" -->  
  10.   <!-- 还要注意FrameLayout标签的位置,要写在TabWidget标签的前面 -->  
  11.   <FrameLayout  
  12.    android:id="@android:id/tabcontent"  
  13.    android:layout_weight="1"   
  14.    android:layout_width="fill_parent"  
  15.    android:layout_height="fill_parent" />  
  16.   
  17.   <TabWidget   
  18.    android:id="@android:id/tabs"  
  19.    android:layout_alignParentBottom="true"   
  20.    android:layout_width="fill_parent"  
  21.    android:layout_height="wrap_content" />  
  22.  </LinearLayout>  
  23. </TabHost>  


 

java源码

[html] view plaincopy
  1. import android.app.TabActivity;        
  2. import android.os.Bundle;     
  3. import android.widget.TabHost;     
  4. import android.widget.Toast;     
  5. import android.widget.TabHost.OnTabChangeListener;  
  6.   
  7. public class TabWidgetActivity extends TabActivity   
  8. {  
  9.     TabHost tabhost;     
  10.     @Override    
  11.     public void onCreate(Bundle savedInstanceState)  
  12.     {     
  13.         super.onCreate(savedInstanceState);     
  14.         setContentView(R.layout.main);     
  15.           
  16.         //取得TabHost对象     
  17.           tabhost = getTabHost();     
  18.           
  19.         //为TabHost添加标签     
  20.          //新建一个newTabSpec(newTabSpec)     
  21.         //设置其标签和图标(setIndicator)     
  22.          //设置内容(setContent)     
  23.           tabhost.addTab(tabhost.newTabSpec("tab1")     
  24.                 .setIndicator("TAB 1",getResources().getDrawable(R.drawable.icon))    
  25.                 .setContent(R.id.text1));     
  26.          tabhost.addTab(tabhost.newTabSpec("tab2")     
  27.                 .setIndicator("TAB 2",getResources().getDrawable(R.drawable.icon))    
  28.                 .setContent(R.id.text2));     
  29.          tabhost.addTab(tabhost.newTabSpec("tab3")     
  30.                 .setIndicator("TAB 3",getResources().getDrawable(R.drawable.icon))    
  31.                 .setContent(R.id.text3));     
  32.          //设置TabHost的背景颜色     
  33.            //tabhost.setBackgroundColor(Color.argb(150,22,70,150));     
  34.          //设置TabHost的背景图片资源     
  35.            tabhost.setBackgroundResource(R.drawable.bg0);     
  36.         //设置当前显示哪个标签     
  37.            tabhost.setCurrentTab(0);     
  38.           
  39.         //标签切换事件处理,setOnTabChangedListener     
  40.         tabhost.setOnTabChangedListener(new OnTabChangeListener()     
  41.         {     
  42.             public void onTabChanged(String tabId)     
  43.             {               
  44.                 switch(tabhost.getCurrentTab())  
  45.                 {  
  46.                 case 0:  
  47.                           tabhost.setBackgroundResource(R.drawable.bg0);  
  48.                          Toast.makeText(getApplicationContext(), "当前标签为第一个页面", Toast.LENGTH_SHORT).show();  
  49.                          break;  
  50.                  case 1:  
  51.                          tabhost.setBackgroundResource(R.drawable.bg1);  
  52.                          Toast.makeText(getApplicationContext(), "当前标签为第二个页面", Toast.LENGTH_SHORT).show();  
  53.                          break;  
  54.                 case 2:  
  55.                          tabhost.setBackgroundResource(R.drawable.bg2);  
  56.                          Toast.makeText(getApplicationContext(), "当前标签为第三个页面", Toast.LENGTH_SHORT).show();  
  57.                          break;  
  58.                 }                              
  59.             }     
  60.         });      
  61.     }     
  62. }  
0 0
原创粉丝点击