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

Android开发教程:底部Tab的两种实现方式

[日期:2012-01-29] 来源:Linux社区  作者:liangruijun [字体: ]

第二种:

在LinerLayout布局里面嵌套FrameLayout和RelativeLayout布局,将TabWidget放置在RelativeLayout里面,之后设置RelativeLayout的Android:layout_alignParentBottom="true" 属性,这个属性的功能是将TabWidget置于父元素(也就是LinerLayout)的底部。这样就能将Tab置于底部了。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout 
  3.     xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:orientation="vertical" 
  5.     android:layout_width="fill_parent" 
  6.     android:layout_height="fill_parent"> 
  7.     <TabHost 
  8.         android:id="@+id/tabhost" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="fill_parent"> 
  11.         <FrameLayout 
  12.             android:id="@android:id/tabcontent" 
  13.             android:layout_width="fill_parent" 
  14.             android:layout_height="fill_parent" 
  15.             android:paddingBottom="62px" 
  16.             > 
  17.             <TextView 
  18.                 android:id="@+id/tab1" 
  19.                 android:layout_width="fill_parent" 
  20.                 android:layout_height="fill_parent" 
  21.                 android:text="这是TabOne" 
  22.                 /> 
  23.             <TextView 
  24.                 android:id="@+id/tab2" 
  25.                 android:layout_width="fill_parent" 
  26.                 android:layout_height="fill_parent" 
  27.                 android:text="这是TabTwo"/> 
  28.         </FrameLayout> 
  29.         <RelativeLayout 
  30.             android:layout_width="fill_parent" 
  31.             android:layout_height="fill_parent" 
  32.             > 
  33.             <TabWidget 
  34.                 android:id="@android:id/tabs" 
  35.                 android:layout_alignParentBottom="true" 
  36.                 android:layout_width="fill_parent" 
  37.                 android:layout_height="65.0px"   
  38.                 android:background="@drawable/tab_bg"             
  39.                 /> 
  40.         </RelativeLayout> 
  41.     </TabHost> 
  42. </LinearLayout> 

main.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello" 
  11.     /> 
  12. </LinearLayout> 

TabHostActivity.java

  1. package com.lingdududu.test;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TabHost;  
  6.  
  7. public class TabHostActivity extends Activity {  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.tabs);  
  11.         TabHost tabs=(TabHost)findViewById(R.id.tabhost);  
  12.           
  13.         tabs.setup();  
  14.           
  15.         TabHost.TabSpec spec=tabs.newTabSpec("tag1");  
  16.         spec.setContent(R.id.tab1);  
  17.         spec.setIndicator("TabOne");  
  18.         tabs.addTab(spec);  
  19.           
  20.         spec=tabs.newTabSpec("tag2");  
  21.         spec.setContent(R.id.tab2);  
  22.         spec.setIndicator("TabTwo");  
  23.         tabs.addTab(spec);  
  24.           
  25.         tabs.setCurrentTab(0);  
  26.     }  
  27. }  
  28.  

效果图:

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

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款