【Android进阶学习】底部Tab的两种实现方式

简介:

先写上代码,稍后在写分析的...................

第一种:

下面的tabs.xml布局文件中,整个布局是垂直显示的,分为FrameLayout和TabWidget上下两部分,在FrameLayout 布局里面使用layout_weight=“1” ,而TabWidget没有设置这个属性,那就默认为0。那么在这布局中,FrameLayout 就按比例分得整个屏幕的3/4,而没有设置layout_weight属性的TabWidget只是占用刚好能显示自己空间大小的位置。这样的话,就能达到就Tab置于底部了。

layout_weight具体可以看看http://liangruijun.blog.51cto.com/3061169/632532里面的FrameLayout 布局

tabs.xml


 
 
  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"   
  4.    android:layout_width="fill_parent"     
  5.    android:layout_height="fill_parent" 
  6.    >     
  7.    <LinearLayout   
  8.       android:orientation="vertical"     
  9.       android:layout_width="fill_parent"   
  10.       android:layout_height="fill_parent" 
  11.       >     
  12.       <FrameLayout   
  13.           android:id="@android:id/tabcontent"     
  14.           android:layout_width="fill_parent"      
  15.           android:layout_height="match_parent"     
  16.           android:layout_weight="1"   
  17.           >    
  18.           <TextView   
  19.               android:id="@+id/view1" 
  20.               android:layout_width="wrap_content" 
  21.               android:layout_height="wrap_content" 
  22.               android:text="nihao" 
  23.               /> 
  24.           <TextView   
  25.               android:id="@+id/view2" 
  26.               android:layout_width="wrap_content" 
  27.               android:layout_height="wrap_content" 
  28.               android:text="nihenhao" 
  29.               /> 
  30.       </FrameLayout>     
  31.       <TabWidget   
  32.           android:id="@android:id/tabs"     
  33.           android:layout_width="fill_parent"      
  34.           android:layout_height="wrap_content"   
  35.           />     
  36.     </LinearLayout>     
  37. </TabHost>  

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> 

TestHostActivity.java


 
 
  1. package com.lingdududu.test;  
  2.  
  3. import android.app.TabActivity;  
  4. import android.os.Bundle;  
  5. import android.widget.TabHost;  
  6.  
  7. public class TestHostActivity extends TabActivity {  
  8.     /** Called when the activity is first created. */ 
  9.     @Override 
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.tabs);  
  13.         TabHost tabhost =  getTabHost();    
  14.         tabhost.addTab(tabhost.newTabSpec("111").setIndicator("view1").setContent(R.id.view1));    
  15.         tabhost.addTab(tabhost.newTabSpec("222").setIndicator("view2").setContent(R.id.view2));    
  16.     }  

效果:

 

第二种:

在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.  

效果图:


本文转自 lingdududu 51CTO博客,原文链接: 

http://blog.51cto.com/liangruijun/747173


相关文章
|
6月前
|
Web App开发 编解码 视频直播
视频直播技术干货(十二):从入门到放弃,快速学习Android端直播技术
本文详细介绍了Android端直播技术的全貌,涵盖了从实时音视频采集、编码、传输到解码与播放的各个环节。文章还探讨了直播中音视频同步、编解码器选择、传输协议以及直播延迟优化等关键问题。希望本文能为你提供有关Andriod端直播技术的深入理解和实践指导。
145 0
|
7月前
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
102 7
第一个安卓项目 | 中国象棋demo学习
|
7月前
|
Android开发
Android学习 —— 测试init.rc中的条件触发的处理顺序
Android学习 —— 测试init.rc中的条件触发的处理顺序
|
8月前
|
搜索推荐 Android开发
学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?
本文分享了不同价位电脑配置对于编译AOSP安卓系统源代码的影响,提供了从6000元到更高价位的电脑配置实例,并比较了它们的编译时间,以供学习AOSP源代码时电脑配置选择的参考。
530 0
学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?
|
9月前
|
XML Android开发 数据格式
Android 中如何设置activity的启动动画,让它像dialog一样从底部往上出来
在 Android 中实现 Activity 的对话框式过渡动画:从底部滑入与从顶部滑出。需定义两个 XML 动画文件 `activity_slide_in.xml` 和 `activity_slide_out.xml`,分别控制 Activity 的进入与退出动画。使用 `overridePendingTransition` 方法在启动 (`startActivity`) 或结束 (`finish`) Activity 时应用这些动画。为了使前 Activity 保持静止,可定义 `no_animation.xml` 并在启动新 Activity 时仅设置新 Activity 的进入动画。
276 12
|
9月前
|
Android开发 UED
Android采用Scroller实现底部二楼效果
Android采用Scroller实现底部二楼效果
85 0
Android采用Scroller实现底部二楼效果
|
10月前
|
存储 算法 Java
Android 进阶——代码插桩必知必会&ASM7字节码操作
Android 进阶——代码插桩必知必会&ASM7字节码操作
527 0
|
10月前
|
XML API Android开发
android上FragmentTabHost实现自定义Tab Indicator
android上FragmentTabHost实现自定义Tab Indicator
61 1
|
10月前
|
Android开发 UED
|
11月前
|
存储 定位技术 开发工具
Android 开发前的设计,Android之内存泄漏调试学习与总结
Android 开发前的设计,Android之内存泄漏调试学习与总结

热门文章

最新文章