<Android>tab选项卡

1.继承TabActivity实现

a)         在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件

b)        Activity要继承TabActivity

c)         调用TabActivity的getTabHost()方法获得TabHost对象

d)        通过TabHost创建Tab选项

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();

                               }

                        }

        );

    }

}

2.Tab的内容还可以通过实现一个接口TabHost.TabContentFactory的createTabContent方法来指定

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;

   }

}
原文地址:https://www.cnblogs.com/lshs/p/4421052.html