In a FrameLayout increase in activity_main.xml, to display the contents Fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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_weight="1"
android:layout_height="0dip"
>
<Button android:id="@+id/add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加选项卡"
android:textSize="20dp"/>
<Button android:id="@+id/remove_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除选项卡"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
Creating tab_content.xml,
<?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:textSize="20dp">
</TextView>
ActionBarTabsActivity.java, the content class MyTabListener for processing tab-related events, content TabContentFragment used to implement custom Fragment:
package com.example.demotab;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ActionBarTabsActivity extends Activity {
static int G = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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 view) {
String title="选项卡:"+bar.getTabCount();
Tab tab = bar.newTab();
tab.setText(title);
tab.setTabListener(new MyTabListener(new TabContentFragment()));
bar.addTab(tab);
}
});
Button removeTab = (Button)findViewById(R.id.remove_tab);
removeTab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(bar.getTabCount()>0){
bar.removeTabAt(bar.getTabCount()-1);
}else{
Toast.makeText(ActionBarTabsActivity.this, "已无Tab可删",Toast.LENGTH_LONG).show();
}
}
});
}
private class MyTabListener implements TabListener{
private TabContentFragment fragment;
public MyTabListener(TabContentFragment fragment){
this.fragment = fragment;
}
public void onTabReselected(Tab tab,FragmentTransaction ft){
}
//处理选择选项卡的事件,增加Fragment
public void onTabSelected(Tab tab,FragmentTransaction ft){
ft.add(R.id.frameLayout, fragment);
}
//处理不选择选项卡的事件,移除Fragment
public void onTabUnselected(Tab tab,FragmentTransaction ft){
ft.remove(fragment);
}
}
public static class TabContentFragment extends Fragment{
public TabContentFragment(){
super();
}
@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("TextTab"+ActionBarTabsActivity.G++);
return fragView;
}
}
}
Reproduced in: https: //my.oschina.net/u/2552902/blog/543890