这回要介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用。Android的Tab控件使用起来有点奇怪,必须包含和按照以下的顺序:


TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent)。

接下来贴出本例运行的截图:


tab.xml的源码:

<TabHost     xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_gravity="top" >            <TabWidget        android:id="@android:id/tabs"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:gravity="top" >    </TabWidget><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <FrameLayout        android:id="@android:id/tabcontent"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true">        <LinearLayout            android:id="@+id/Tab1"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical" >            <EditText                android:id="@+id/edtTab1"                android:layout_width="fill_parent"                android:layout_height="wrap_content" />            <Button                android:id="@+id/btnTab1"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:text="Tab1" />        </LinearLayout>        <LinearLayout            android:id="@+id/Tab2"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical" >            <EditText                android:id="@+id/edtTab2"                android:layout_width="fill_parent"                android:layout_height="wrap_content" />            <Button                android:id="@+id/btnTab2"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:text="Tab2" />        </LinearLayout>    </FrameLayout>       </RelativeLayout></TabHost>

TabDemo.java

package com.tabhost;import android.app.TabActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class TabDemo extends TabActivity {//基于TabActivity构建Button btnTab1,btnTab2;EditText edtTab1,edtTab2;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)     {    super.onCreate(savedInstanceState);        setContentView(R.layout.tab);                TabHost tabs = getTabHost();                //设置Tab1        TabSpec tab1 = tabs.newTabSpec("tab1");        tab1.setIndicator("tab1");// 设置tab1的名称        tab1.setContent(R.id.Tab1);// 关联控件        tabs.addTab(tab1);// 添加tab1                btnTab1=(Button)this.findViewById(R.id.btnTab1);        edtTab1=(EditText)this.findViewById(R.id.edtTab1);        btnTab1.setOnClickListener(new ClickEvent());                //设置Tab2        TabSpec tab2 = tabs.newTabSpec("tab2");        tab2.setIndicator("tab2");              tab2.setContent(R.id.Tab2);            tabs.addTab(tab2);                                btnTab2=(Button)this.findViewById(R.id.btnTab2);        edtTab2=(EditText)this.findViewById(R.id.edtTab2);        btnTab2.setOnClickListener(new ClickEvent());                tabs.setCurrentTab(0);    }        class ClickEvent implements View.OnClickListener     {@Overridepublic void onClick(View v) {if(v==btnTab1){edtTab1.setText("tab1");}else if(v==btnTab2){edtTab2.setText("tab2");}}        }}

更多相关文章

  1. Android(安卓)中自定义控件和属性(attr.xml,declare-styleable,T
  2. Android五大布局介绍&属性设置大全
  3. Android(安卓)自定义View--ProgressBar篇(三)
  4. 解决eclipse里android无法关联源码办法
  5. Android(安卓)WebView那些坑之上传文件
  6. Android开发实战(二十一):浅谈android:clipChildren属性
  7. windows中下载android源码的方法 附下载脚本
  8. android去掉EditView的默认焦点问题
  9. Android日记之2012\01\13

随机推荐

  1. [Android]判断一个app运行在foreground
  2. Android中SQLite增删改查的方法(Android开
  3. 如何避免输入法弹出时遮挡住按钮或输入框
  4. make: *** [out/host/linux-x86/obj/EXEC
  5. spotlight android app which likes spot
  6. 【Android】使RecyclerView 支持setEmpty
  7. Eclipse 开发 Android, Hello FormStuff(
  8. Android的下拉式選單
  9. Android(安卓)自制照相机
  10. android 通过ContentResolver获得联系人