你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发:巧妙运用ViewStub写出类似Tab选项卡

[日期:2011-08-20] 来源:Linux社区  作者:jamesliulyc [字体:大 中 小]

第二步:就是建立4个xml布局文件,里面可以只写一个TextView,命名为

btn1_layout.xml,btn2_layout.xml,btn3_layout.xml,btn4_layout.xml.类似如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.      xmlns:Android="http://schemas.android.com/apk/res/android"  
  4.       android:layout_width="fill_parent"   
  5.       android:layout_height="fill_parent">  
  6.       <TextView   
  7.         android:layout_width="fill_parent"   
  8.         android:layout_height="fill_parent"  
  9.         android:text="Button 1"  
  10.         android:textSize="36sp"  
  11.         android:textColor="#4a9ad8"  
  12.       />  
  13. </LinearLayout>  

第三步: 

 将下列代码插入到第一步中main.xml中,位于TableLayout之上

  1. <ViewStub   
  2.             android:id="@+id/btn1ViewStub"    
  3.             android:layout="@layout/btn1_layout"  
  4.             android:layout_width="fill_parent"  
  5.             android:layout_height="fill_parent"   
  6.         />  
  7.     <ViewStub   
  8.             android:id="@+id/btn2ViewStub"    
  9.             android:layout="@layout/btn2_layout"  
  10.             android:layout_width="fill_parent"  
  11.             android:layout_height="fill_parent"   
  12.         />  
  13.     <ViewStub   
  14.             android:id="@+id/btn3ViewStub"    
  15.             android:layout="@layout/btn3_layout"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="fill_parent"   
  18.         />  
  19.     <ViewStub   
  20.             android:id="@+id/btn4ViewStub"    
  21.             android:layout="@layout/btn4_layout"  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="fill_parent"   
  24.         />  

第四步:Activity中,产生点击事件后,首先要将所有的ViewStub设置成不可见,否则将会出问题(你可以试试),java代码如下,具体就不解释了,能用ViewStub相信能看懂。

  1. package com.tab.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.ViewStub;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends Activity {  
  11.       
  12.     private ViewStub[] viewStub = new ViewStub[4];  
  13.     private Button currentBtn;  
  14.     private Button lastBtn;  
  15.       
  16.     private int[] tabBtnIds = {R.id.btn1, R.id.btn2,  
  17.             R.id.btn3, R.id.btn4};  
  18.       
  19.     private Button[] tabBtn = new Button[4];  
  20.       
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         viewStub[0] = (ViewStub) findViewById(R.id.btn1ViewStub);  
  26.         viewStub[1] = (ViewStub) findViewById(R.id.btn2ViewStub);  
  27.         viewStub[2] =(ViewStub) findViewById(R.id.btn3ViewStub);  
  28.         viewStub[3] = (ViewStub) findViewById(R.id.btn4ViewStub);  
  29.         currentBtn = (Button) findViewById(R.id.btn2);  
  30.         TabBtnClickListener tabBtnListener = new TabBtnClickListener();  
  31.         for(int i=0; i<tabBtnIds.length; i++) {  
  32.             tabBtn[i] = (Button) findViewById(tabBtnIds[i]);  
  33.             tabBtn[i].setOnClickListener(tabBtnListener);  
  34.         }  
  35.           
  36.     }  
  37.       
  38.     class TabBtnClickListener implements View.OnClickListener {  
  39.   
  40.         @Override  
  41.         public void onClick(View v) {  
  42.             lastBtn = currentBtn;  
  43.             currentBtn = (Button) v;  
  44.             if(currentBtn.getId() == lastBtn.getId()) {  
  45.                 return;  
  46.             }  
  47.             currentBtn.setBackgroundColor(Color.BLUE);  
  48.             lastBtn.setBackgroundColor(Color.GRAY);  
  49.             int currentIndex = -1;  
  50.             switch(currentBtn.getId()) {  
  51.                 case R.id.btn1:  
  52.                     currentIndex = 0;  
  53.                     break;  
  54.                 case R.id.btn2:  
  55.                     currentIndex = 1;  
  56.                     break;  
  57.                 case R.id.btn3:  
  58.                     currentIndex = 2;  
  59.                     break;  
  60.                 case R.id.btn4:  
  61.                     currentIndex = 3;  
  62.                     break;  
  63.             }  
  64.             for(int i=0; i<viewStub.length; i++) {  
  65.                 viewStub[i].setVisibility(View.INVISIBLE);  
  66.             }  
  67.             for(int i=0; i<viewStub.length; i++) {  
  68.                 if(currentIndex == -1) {  
  69.                     break;  
  70.                 }  
  71.                 if(currentIndex != i) {  
  72.                     viewStub[i].setVisibility(View.INVISIBLE);  
  73.                 } else {  
  74.                     viewStub[i].setVisibility(View.VISIBLE);  
  75.                 }  
  76.             }  
  77.         }  
  78.     }  
  79. }  

最后截个图,比上面那个通讯录难看,但是Activity基本上是一样的。

源码下载地址

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2011年资料/Android入门教程/Android开发:巧妙运用ViewStub写出类似Tab选项卡/

linux
相关资讯       Android开发 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/10/6 15:49:22
你服务器里有用的东西好多,下载啦