Android中两种实现底部Tab的方法(1)

  1.        android:layout\_width\="fill\_parent"      
    
  2.        android:layout\_height\="match\_parent"     
    
  3.        android:layout\_weight\="1"   
    
  4.        \>    
    
  5.        <TextView   
    
  6.            android:id\="@+id/view1" 
    
  7.            android:layout\_width\="wrap\_content" 
    
  8.            android:layout\_height\="wrap\_content" 
    
  9.            android:text\="nihao" 
    
  10.            /> 
    
  11.        <TextView   
    
  12.            android:id\="@+id/view2" 
    
  13.            android:layout\_width\="wrap\_content" 
    
  14.            android:layout\_height\="wrap\_content" 
    
  15.            android:text\="nihenhao" 
    
  16.            /> 
    
  17.    </FrameLayout\>     
    
  18.    <TabWidget   
    
  19.        android:id\="@android:id/tabs"     
    
  20.        android:layout\_width\="fill\_parent"      
    
  21.        android:layout\_height\="wrap\_content"   
    
  22.        />     
    
  23.  </LinearLayout\>     
    
  24. </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. import android.app.TabActivity;

  3. import android.os.Bundle;

  4. import android.widget.TabHost;

  5. public class TestHostActivity extends TabActivity {

  6.  /\*\* Called when the activity is first created. \*/ 
    
  7.  @Override 
    
  8.  public void onCreate(Bundle savedInstanceState) {  
    
  9.      super.onCreate(savedInstanceState);  
    
  10.      setContentView(R.layout.tabs);  
    
  11.      TabHost tabhost =  getTabHost();    
    
  12.      tabhost.addTab(tabhost.newTabSpec("111").setIndicator("view1").setContent(R.id.view1));    
    
  13.      tabhost.addTab(tabhost.newTabSpec("222").setIndicator("view2").setContent(R.id.view2));    
    
  14.  }  
    
  15. }

效果:

第二种:

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

  1.          android:paddingBottom\="62px" 
    
  2.          \> 
    
  3.          <TextView 
    
  4.              android:id\="@+id/tab1" 
    
  5.              android:layout\_width\="fill\_parent" 
    
  6.              android:layout\_height\="fill\_parent" 
    
  7.              android:text\="这是TabOne" 
    
  8.              /> 
    
  9.          <TextView 
    
  10.              android:id\="@+id/tab2" 
    
  11.              android:layout\_width\="fill\_parent" 
    
  12.              android:layout\_height\="fill\_parent" 
    
  13.              android:text\="这是TabTwo"/> 
    
  14.      </FrameLayout\> 
    
  15.      <RelativeLayout 
    
  16.          android:layout\_width\="fill\_parent" 
    
  17.          android:layout\_height\="fill\_parent" 
    
  18.          \> 
    
  19.          <TabWidget 
    
  20.              android:id\="@android:id/tabs" 
    
  21.              android:layout\_alignParentBottom\="true" 
    
  22.              android:layout\_width\="fill\_parent" 
    
  23.              android:layout\_height\="65.0px"   
    
  24.              android:background\="@drawable/tab\_bg"             
    
  25.              /> 
    
  26.      </RelativeLayout\> 
    
  27.  </TabHost\> 
    
  28. </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>

fill_parent"

  1.  android:layout\_height\="fill\_parent" 
    
  2.  \> 
    
  3. <TextView

  4.  android:layout\_width\="fill\_parent"   
    
  5.  android:layout\_height\="wrap\_content"   
    
  6.  android:text\="@string/hello" 
    
  7.  /> 
    
  8. </LinearLayout>

  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值