android用户界面-组件Widget-选项卡Tab

使用Tab的步骤:
1、在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件。

2、Activity要继承TabActivity。

3、调用TabActivity的getTabHost()方法获得TabHost对象。

4、通过TabHost创建Tab选项。

 

/Chapter04_UI_Tab01/src/com/amaker/test/MainActivity.java

代码
   
     
package com.amaker.test;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);

/* requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
*/

TabHost th
= getTabHost();

LayoutInflater.from(
this ).inflate(R.layout.main, th.getTabContentView(), true );

th.addTab(th.newTabSpec(
" all " ).setIndicator( " 所有通话记录 " ).setContent(R.id.TextView01));
th.addTab(th.newTabSpec(
" ok " ).setIndicator( " 已接来电 " ).setContent(R.id.TextView02));
th.addTab(th.newTabSpec(
" cancel " ).setIndicator( " 未接来电 " ).setContent(R.id.TextView03));


th.setOnTabChangedListener(
new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Toast.makeText(MainActivity.
this , tabId, Toast.LENGTH_LONG).show();
}
}
);



}
}

 

/Chapter04_UI_Tab01/res/layout/main.xml

代码
   
     
<? xml version="1.0" encoding="utf-8" ?>

< FrameLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:id
="@+id/FrameLayout01"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" >

< TextView
android:id ="@+id/TextView01"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="所有通话记录" ></ TextView >

< TextView
android:id ="@+id/TextView02"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="已接来电" ></ TextView >

< TextView
android:id ="@+id/TextView03"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="未接来电" ></ TextView >

</ FrameLayout >


 

 

 

通过实现一个接口TabHost.TabContentFactory的createTabContent方法来制定Tab的内容

/Chapter04_UI_Tab02/src/com/amaker/test/MainActivity.java

代码
   
     
package com.amaker.test;

import java.util.ArrayList;
import java.util.List;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;

public class MainActivity extends TabActivity implements
TabHost.TabContentFactory {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
TabHost th
= getTabHost();
th.addTab(th.newTabSpec(
" all " ).setIndicator( " 所有通话记录 " ).setContent( this ));
th.addTab(th.newTabSpec(
" ok " ).setIndicator( " 已接来电 " ).setContent( this ));
th
.addTab(th.newTabSpec(
" cancel " ).setIndicator( " 未接来电 " )
.setContent(
this ));
}

public View createTabContent(String tag) {
ListView lv
= new ListView( this );
List
< String > list = new ArrayList < String > ();
list.add(tag);
if (tag.equals( " all " )){
list.add(
" tom " );
list.add(
" kite " );
list.add(
" rose " );
}
else if (tag.equals( " ok " )){
list.add(
" tom " );
list.add(
" kite " );
}
else {
list.add(
" rose " );
}

ArrayAdapter adapter
= new ArrayAdapter( this ,
android.R.layout.simple_list_item_checked, list);
lv.setAdapter(adapter);
return lv;
}
}

 

/Chapter04_UI_Tab02/res/layout/main.xml

代码
   
     
<? 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"
>
< TextView
android:layout_width ="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/hello"
/>
</ LinearLayout >

 

你可能感兴趣的:(android)