动作栏提供基于选项卡模式的导航方式,它允许用户在一个Activity中,切换不同Framement。同时,针对用户选择选项卡事件,还专门定义了一个事件监听器。
在ActionBar类中,与Tab相关的常用方法如下:
增加选项卡:add Tab
Public abstract void addTab(ActionBar.Tab tab)
获得当前选择的选项卡:getSelectedTab
Public abstract ActionBar.Tab getSelectedTab()
用来获得指定索引位置的选项卡:getTabAt
Public abstract ActionBar.Tab getTabAt(int index)
获得选项卡的个数:getTabCount
Public abstract int getTabCount()
设置选项卡被选中:selectTab
Public abstract void selectTab(ActionBar.Tab tab)
在ActionBar类中,定义了一个内部接口TabListener,用来处理动作栏上选项卡相关事件,定义方法如下:
Public abstract void onTabReselected(ActionBar.Tab tab,FragmentTransaction ft)
用于处理选项卡再次被选中事件
Public abstract void onTabSelected(ActionBar.Tab tab,FragmentTransaction ft)
用于处理选项卡选中事件
Public abstract void onTabUnselected(ActionBar.Tab tab,FragmentTransaction ft)
用于处理退出选择状态选项卡事件
Process:
1.Edit res/layout/mainactivity.xml file, add a for displaying the content of Fragment. Then, add a . Add two buttons in it and change the default property.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:baselineAligned="false" >
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<Button
android:id="@+id/add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_tab"
android:textColor="@android:color/white"
android:textSize="20dp" />
<Button
android:id="@+id/remove_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/remove_tab"
android:textColor="@android:color/white"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
2.Create tab_content.xml in /res/layout package, including a TextView
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@android:color/white"
android:textSize="20dp" >
</TextView>
3.Create ActionBarTabsActivity class which extends Activity class. In the method onCreate(), achieve button controls and implement the function of adding and deleteing Tabs. Create MyTabListener inner class to deal with Tabs events and TabContentFragment to implement custom Fragment.
public class ActionBarTabsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);// 调用父类方法
setContentView(R.layout.main);// 应用布局文件
final ActionBar bar = getActionBar();// 获得动作栏
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);// 设置动作栏导航模式
Button addTab = (Button) findViewById(R.id.add_tab);
addTab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = "选项卡:" + bar.getTabCount();// 定义选项卡标题
Tab tab = bar.newTab();// 新建选项卡
tab.setText(title);// 设置选项卡标题
tab.setTabListener(new MyTabListener(new TabContentFragment(title)));// 设置选项卡事件监听器
bar.addTab(tab);// 增加选项卡
}
});
Button removeTab = (Button) findViewById(R.id.remove_tab);
removeTab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bar.removeTabAt(bar.getTabCount() - 1);// 删除最后一个选项卡
}
});
}
private class MyTabListener implements TabListener {
private TabContentFragment fragment;
public MyTabListener(TabContentFragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {// 处理选择选项卡事件
ft.add(R.id.frameLayout, fragment);// 增加Fragment
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {// 处理不选择选项卡事件
ft.remove(fragment);// 移除Fragment
}
}
private class TabContentFragment extends Fragment {
private String message;
public TabContentFragment(String message) {
this.message = message;// 获得需要显示的字符串
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.tab_content, container, false);
TextView text = (TextView) fragView.findViewById(R.id.content);// 获得文本框控件
text.setText(message);// 显示字符串
return fragView;// 返回视图
}
}
}