看到很多热门的Android程序(如:新浪微博、腾讯微博、京东商城、淘宝、当当等等)使用选项卡风格作为程序界面的主框架结构,而 Android的选项卡控件默认是按钮在上方的。我在网上看到有多种实现方法,这里提供一种个人觉得比较简单的。由于我对Android开发所知甚少,方 法的优劣目前不好评价,欢迎各位提供更好的思路。

 

 

 

主要原理:设置 TabWidget 控件的 android:layout_alignParentBottom="true" 实现。

 

main.xml  
            
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost android:id="@+id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <RelativeLayout android:orientation="vertical"  
  6.         android:layout_width="fill_parent" android:layout_height="fill_parent">  
  7.         <TabWidget android:id="@android:id/tabs"  
  8.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  9.             android:layout_alignParentBottom="true" />  
  10.         <FrameLayout android:id="@android:id/tabcontent"  
  11.             android:layout_width="fill_parent" android:layout_height="fill_parent">  
  12.             <LinearLayout android:id="@+id/tab1"  
  13.                 android:layout_width="fill_parent" android:layout_height="fill_parent"  
  14.                 androidrientation="vertical">  
  15.                 <TextView android:id="@+id/view1" android:layout_width="wrap_content"  
  16.                     android:layout_height="wrap_content" android:text="@string/textView_1" />  
  17.             LinearLayout>  
  18.             <LinearLayout android:id="@+id/tab2"  
  19.                 android:layout_width="fill_parent" android:layout_height="fill_parent"  
  20.                 androidrientation="vertical">  
  21.                 <TextView android:id="@+id/view2" android:layout_width="wrap_content"  
  22.                     android:layout_height="wrap_content" android:text="@string/textView_2" />  
  23.             LinearLayout>  
  24.             <LinearLayout android:id="@+id/tab3"  
  25.                 android:layout_width="fill_parent" android:layout_height="fill_parent"  
  26.                 androidrientation="vertical">  
  27.                 <TextView android:id="@+id/view3" android:layout_width="wrap_content"  
  28.                     android:layout_height="wrap_content" android:text="@string/textView_3" />  
  29.             LinearLayout>  
  30.             <LinearLayout android:id="@+id/tab4"  
  31.                 android:layout_width="fill_parent" android:layout_height="fill_parent"  
  32.                 androidrientation="vertical">  
  33.                 <TextView android:id="@+id/view4" android:layout_width="wrap_content"  
  34.                     android:layout_height="wrap_content" android:text="@string/textView_4" />  
  35.             LinearLayout>  
  36.         FrameLayout>  
  37.     RelativeLayout>  
  38. TabHost>  
  39.  
TabDemo1
            
  1. package com.focusmobi.TabDemo1; 
  2.  
  3. import android.app.Activity; 
  4. import android.os.Bundle; 
  5. import android.widget.TabHost; 
  6.  
  7. public class TabDemo1 extends Activity { 
  8.     /** Called when the activity is first created. */ 
  9.     @Override 
  10.     public void onCreate(Bundle icicle) { 
  11.         super.onCreate(icicle); 
  12.          
  13.         setContentView(R.layout.main); 
  14.         setTitle("TabWidget Demo"); 
  15.          
  16.         TabHost tabs = (TabHost) findViewById(R.id.tabhost); 
  17.         tabs.setup(); 
  18.          
  19.         TabHost.TabSpec spec = tabs.newTabSpec("tab1"); 
  20.         spec.setContent(R.id.tab1); 
  21.         spec.setIndicator("主页"); 
  22.         tabs.addTab(spec); 
  23.          
  24.         spec = tabs.newTabSpec("tab2"); 
  25.         spec.setContent(R.id.tab2); 
  26.         spec.setIndicator("经济"); 
  27.         tabs.addTab(spec); 
  28.          
  29.         spec = tabs.newTabSpec("tab3"); 
  30.         spec.setContent(R.id.tab3); 
  31.         spec.setIndicator("汽车"); 
  32.         tabs.addTab(spec); 
  33.          
  34.         spec = tabs.newTabSpec("tab4"); 
  35.         spec.setContent(R.id.tab4); 
  36.         spec.setIndicator("科技"); 
  37.         tabs.addTab(spec);  
  38.          
  39.         tabs.setCurrentTab(0); 
  40.     } 

 

本文只是解决了将选项卡按钮放在屏幕下方,至于如何美化按钮使程序看起来更加赏心悦目已经超出了本文范围,以后我们再讨论。

 

×××:android_tabwidget1_src.rar

 

 

作者:黎波
博客: http://bobli.cnblogs.com/
日期:2011年5月9日

 

更多相关文章

  1. Android(安卓)UI框架 Android(安卓)UI控件类简介 android5大布局
  2. android仿iPhone滚轮控件实现及源码分析(二)
  3. Android(安卓)4.0新增Space及GridLayout初谈
  4. Preference组件探究之Base,Support及AndroidX对比
  5. Android(安卓)实用工具Hierarchy Viewer实战
  6. Android(安卓)XML属性介绍
  7. Android(安卓)实用工具Hierarchy Viewer实战
  8. Android基本控件模板
  9. Android(安卓)如何让EditText不自动获取焦点

随机推荐

  1. Android入门篇四:使用全局变量在Activity
  2. (超详细)android中SqLite数据库的使用(一文
  3. Android(安卓)的 Handler 总结
  4. Android属性动画ValueAnimator源码简单分
  5. Android(安卓)EventBus框架入门
  6. android 自学日记(三) ---Intent
  7. Android和js进行交互
  8. Android_TextView属性XML详解
  9. Android(安卓)资源,国际化,自适应
  10. shape基本用法及全部属性定义