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

来源:互联网 发布:加密软件哪个好 编辑:程序博客网 时间:2024/05/08 21:52

使用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>
0 0
原创粉丝点击