实现ActonBar的tab步骤
1. 激活此项功能
4. 示例
src/com/wind/actionbar/HelloActionBarActivity.java
src/com/wind/actionbar/Tab1Fragment.java
res/layout/main.xml
1. 激活此项功能
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
2. 实现ActionBar.TabListener接口,为增加tab做准备.实现ActionBar.TabListener接口时,应当在类内有个Fragment的引用,这样点击这个Tab时就可以调用对应的Fragment.
class MyTabListener implements TabListener
{
// 接收每个Tab对应的Fragment,操作
private Fragment fragment;
public MyTabListener(Fragment fragment) {
super();
this.fragment = fragment;
}
...
}
3. 增加tab
Tab tab1 = actionBar.newTab().setText("Tab1");
Tab tab2 = actionBar.newTab().setText("Tab2");
// 为每个Tab添加Listener
MyTabListener tab1Listener = new MyTabListener(new Tab1Fragment());
tab1.setTabListener(tab1Listener);
MyTabListener tab2Listener = new MyTabListener(new Tab2Fragment());
tab2.setTabListener(tab2Listener);
// 将Tab加入ActionBar中
actionBar.addTab(tab1);
actionBar.addTab(tab2);
4. 示例
src/com/wind/actionbar/HelloActionBarActivity.java
package com.wind.actionbar;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
public class HelloActionBarActivity extends Activity{
private static final String TAG = "HelloActionBarActivity";
private ActionBar actionBar;
protected boolean mFlagDial = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
setContentView(R.layout.main);
actionBar = getActionBar();
//设置AcitonBar的操作模型
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//去掉title
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
// 生成Tab
Tab tab1 = actionBar.newTab().setText("Tab1");
Tab tab2 = actionBar.newTab().setText("Tab2");
// 为每个Tab添加Listener
MyTabListener tab1Listener = new MyTabListener(new Tab1Fragment());
tab1.setTabListener(tab1Listener);
MyTabListener tab2Listener = new MyTabListener(new Tab2Fragment());
tab2.setTabListener(tab2Listener);
// 将Tab加入ActionBar中
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
/**
* 实现ActionBar.TabListener接口
*/
class MyTabListener implements TabListener
{
// 接收每个Tab对应的Fragment,操作
private Fragment fragment;
public MyTabListener(Fragment fragment) {
super();
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.context, fragment, null);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
}
src/com/wind/actionbar/Tab1Fragment.java
package com.wind.actionbar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.custom_action_view, container, false);
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
src/com/wind/actionbar/Tab2Fragment.java
package com.wind.actionbar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab2Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.two_button_hor, container, false);
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/context"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>