Tabs选项卡在某些情况下使用还是蛮方便的,它可以充当软件界面的导航面板,Tabs默认是放在顶部的,通过自己的设置它可以放在底部,Tabs的使用可以使我们避免通过切换Activity来切换页面,这里我们通过一个Activity就可以实现各个页面的切换,下面是它的实现源码:
复制代码
xml代码如下:
复制代码
运行结果如图:
- package com.android.sss;
-
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TabHost;
- import android.widget.Toast;
- import android.widget.TabWidget;
- import android.widget.TextView;
-
- public class TabsDemo extends Activity {
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.test);
- final TabHost tabs=(TabHost)findViewById(R.id.tabhost);
- final TabWidget tabWidget=(TabWidget) findViewById(android.R.id.tabs);
- //设置tabs的分割线颜色
- tabWidget.setBackgroundColor(Color.RED);
- Button backButton = (Button)findViewById(R.id.backButton);
- Button playingButton = (Button)findViewById(R.id.playingButton);
- backButton.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(TabsDemo.this, "返回", Toast.LENGTH_SHORT).show();
- }
- });
- playingButton.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(TabsDemo.this, "详细信息", Toast.LENGTH_SHORT).show();
- }
- });
- int width =60;
- int height =60;
- tabs.setup();
- tabs.addTab(tabs.newTabSpec("first tab")
- .setIndicator("第一页",null)
- .setContent(R.id.tab1));
- tabs.addTab(tabs.newTabSpec("second tab")
- .setIndicator("第二页",null)
- .setContent(R.id.tab2));
- tabs.addTab(tabs.newTabSpec("third tab")
- .setIndicator("第三页",null)
- .setContent(R.id.tab3)
- );
-
- tabs.setCurrentTab(0);
-
- for (int i =0; i <tabWidget.getChildCount(); i++) {
- /**
- * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果
- */
- tabWidget.getChildAt(i).getLayoutParams().height = height;
- tabWidget.getChildAt(i).getLayoutParams().width = width;
-
-
- /**
- * 设置tab中标题文字的颜色,不然默认为黑色
- */
- final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
- tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
- tv.setTextSize(15);
- tv.setGravity(Gravity.TOP);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_weight="3"
- android:layout_width="fill_parent"
- android:layout_gravity="top"
- android:background="@drawable/white"
- android:layout_height="140dip">
- <Button android:id="@+id/backButton"
- android:gravity="left"
- android:layout_width="80dip"
- android:layout_height="fill_parent"
- android:paddingLeft="10dip"
- android:layout_weight="1"
- android:background="@drawable/white"
- android:textSize="20dip"
- android:text="返回" />
- <Button android:id="@+id/playingButton"
- android:layout_width="80dip"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:gravity="right"
- android:paddingRight="10dip"
- android:background="@drawable/white"
- android:textSize="20dip"
- android:text="详细信息" />
- </LinearLayout>
- <TabHost
- android:id="@+id/tabhost"
- android:layout_weight="1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:paddingBottom="40px">
- <AnalogClock
- android:id="@+id/tab1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_centerHorizontal="true" />
- <Button
- android:id="@+id/tab2"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="A semi-random button" />
- <Button
- android:id="@+id/tab3"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/screen1"
- android:text="testdemo" />
- </FrameLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_alignParentBottom="true"
- android:layout_width="fill_parent"
- android:layout_height="40dip"
- android:layout_marginBottom="10dip"
- android:gravity="bottom"
- />
- </RelativeLayout>
- </TabHost>
- </LinearLayout>