Tabs选项卡放在底部的简单应用

来源:互联网 发布:ubuntu如何启动mysql 编辑:程序博客网 时间:2024/04/28 10:57
 Tabs选项卡在某些情况下使用还是蛮方便的,它可以充当软件界面的导航面板,Tabs默认是放在顶部的,通过自己的设置它可以放在底部,Tabs的使用可以使我们避免通过切换Activity来切换页面,这里我们通过一个Activity就可以实现各个页面的切换,下面是它的实现源码:
  1. package com.android.sss;

  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TabHost;
  9. import android.widget.Toast;
  10. import android.widget.TabWidget;
  11. import android.widget.TextView;

  12. public class TabsDemo extends Activity {
  13.         public void onCreate(Bundle icicle) {   
  14.         super.onCreate(icicle);   
  15.         setContentView(R.layout.test);   
  16.         final TabHost tabs=(TabHost)findViewById(R.id.tabhost);   
  17.         final TabWidget tabWidget=(TabWidget) findViewById(android.R.id.tabs);
  18.         //设置tabs的分割线颜色
  19.         tabWidget.setBackgroundColor(Color.RED);
  20.         Button backButton = (Button)findViewById(R.id.backButton);
  21.         Button playingButton = (Button)findViewById(R.id.playingButton);
  22.         backButton.setOnClickListener(new Button.OnClickListener() {
  23.                         @Override
  24.                         public void onClick(View v) {
  25.                                 Toast.makeText(TabsDemo.this, "返回", Toast.LENGTH_SHORT).show();
  26.                         }
  27.                 });
  28.         playingButton.setOnClickListener(new Button.OnClickListener() {
  29.                         @Override
  30.                         public void onClick(View v) {
  31.                                 Toast.makeText(TabsDemo.this, "详细信息", Toast.LENGTH_SHORT).show();
  32.                         }
  33.                 });
  34.         int width =60;
  35.         int height =60;
  36.         tabs.setup();   
  37.         tabs.addTab(tabs.newTabSpec("first tab")
  38.                 .setIndicator("第一页",null)
  39.                 .setContent(R.id.tab1));
  40.         tabs.addTab(tabs.newTabSpec("second tab")
  41.                 .setIndicator("第二页",null)
  42.                 .setContent(R.id.tab2));
  43.         tabs.addTab(tabs.newTabSpec("third tab")
  44.                 .setIndicator("第三页",null)
  45.                 .setContent(R.id.tab3)
  46.                 );
  47.            
  48.         tabs.setCurrentTab(0);  
  49.         
  50.         for (int i =0; i <tabWidget.getChildCount(); i++) {
  51.             /**
  52.              * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果
  53.              */
  54.             tabWidget.getChildAt(i).getLayoutParams().height = height;
  55.             tabWidget.getChildAt(i).getLayoutParams().width = width;


  56.          /**
  57.           * 设置tab中标题文字的颜色,不然默认为黑色
  58.           */
  59.           final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
  60.           tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
  61.           tv.setTextSize(15);
  62.           tv.setGravity(Gravity.TOP);
  63.     }   
  64. }
复制代码
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.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  7.     android:orientation="horizontal" 
  8.     android:layout_weight="3"
  9.     android:layout_width="fill_parent"
  10.     android:layout_gravity="top" 
  11.     android:background="@drawable/white"
  12.     android:layout_height="140dip">
  13.     <Button android:id="@+id/backButton"  
  14.             android:gravity="left"
  15.             android:layout_width="80dip"  
  16.             android:layout_height="fill_parent" 
  17.             android:paddingLeft="10dip"
  18.             android:layout_weight="1"
  19.             android:background="@drawable/white"
  20.             android:textSize="20dip"
  21.             android:text="返回" /> 
  22.     <Button android:id="@+id/playingButton"  
  23.             android:layout_width="80dip"  
  24.             android:layout_height="fill_parent" 
  25.             android:layout_weight="1"
  26.             android:gravity="right"
  27.             android:paddingRight="10dip"
  28.             android:background="@drawable/white"
  29.             android:textSize="20dip"
  30.             android:text="详细信息" /> 
  31.     </LinearLayout> 
  32.     <TabHost  
  33.         android:id="@+id/tabhost"  
  34.         android:layout_weight="1"
  35.         android:layout_width="fill_parent"  
  36.         android:layout_height="fill_parent">  
  37.         <FrameLayout  
  38.             android:id="@android:id/tabcontent"  
  39.             android:layout_width="fill_parent"  
  40.             android:layout_height="fill_parent"  
  41.             android:paddingBottom="40px">  
  42.             <AnalogClock  
  43.                 android:id="@+id/tab1"  
  44.                 android:layout_width="fill_parent"  
  45.                 android:layout_height="fill_parent"  
  46.                 android:layout_centerHorizontal="true" />  
  47.             <Button  
  48.                 android:id="@+id/tab2"  
  49.                 android:layout_width="fill_parent"  
  50.                 android:layout_height="fill_parent"  
  51.                 android:text="A semi-random button" />  
  52.             <Button  
  53.                 android:id="@+id/tab3"  
  54.                 android:layout_width="fill_parent"  
  55.                 android:layout_height="fill_parent"  
  56.                 android:background="@drawable/screen1"
  57.                 android:text="testdemo" /> 
  58.         </FrameLayout>  
  59.         <RelativeLayout  
  60.             android:layout_width="fill_parent"  
  61.             android:layout_height="fill_parent">  
  62.             <TabWidget  
  63.                 android:id="@android:id/tabs"  
  64.                 android:layout_alignParentBottom="true"  
  65.                 android:layout_width="fill_parent"  
  66.                 android:layout_height="40dip"
  67.                 android:layout_marginBottom="10dip" 
  68.                 android:gravity="bottom"
  69.                 />  
  70.         </RelativeLayout>  
  71.     </TabHost>  
  72. </LinearLayout>
复制代码
运行结果如图: