<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@+id/tabwidget_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/layout_container_tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.app.FragmentTabHost>
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.widget.TabHost.TabSpec;
public class MainActivity extends FragmentActivity {
private FragmentTabHost tabHost;
private Bundle bundle1, bundle2, bundle3, bundle4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
FragmentManager manager = getSupportFragmentManager();
tabHost.setup(this, manager, R.id.layout_container_tabcontent);
TabSpec tabSpec1 = tabHost.newTabSpec("records");
tabSpec1.setIndicator("记录");
bundle1 = new Bundle();
bundle1.putInt("tabIndex", 1);
TabSpec tabSpec2 = tabHost.newTabSpec("contacts");
tabSpec2.setIndicator("联系人");
bundle2 = new Bundle();
bundle2.putInt("tabIndex", 2);
TabSpec tabSpec3 = tabHost.newTabSpec("collections");
tabSpec3.setIndicator("收藏夹");
bundle3 = new Bundle();
bundle3.putInt("tabIndex", 3);
TabSpec tabSpec4 = tabHost.newTabSpec("groups");
tabSpec4.setIndicator("群组");
bundle4 = new Bundle();
bundle4.putInt("tabIndex", 4);
tabHost.addTab(tabSpec1, DummyFragment.class, bundle1);
tabHost.addTab(tabSpec2, DummyFragment.class, bundle2);
tabHost.addTab(tabSpec3, DummyFragment.class, bundle3);
tabHost.addTab(tabSpec4, DummyFragment.class, bundle4);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DummyFragment extends Fragment {
private TextView text_dummyfragment;
private Bundle bundle = null;
private int tabIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = getArguments();
tabIndex = bundle.getInt("tabIndex");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dummy, container, false);
text_dummyfragment = (TextView) view
.findViewById(R.id.text_dummyfragment);
Log.i("", "==" + tabIndex);
switch (tabIndex) {
case 1:
text_dummyfragment.setText("这个是记录页面");
break;
case 2:
text_dummyfragment.setText("这个是联系人页面");
break;
case 3:
text_dummyfragment.setText("这个是收藏夹页面");
break;
case 4:
text_dummyfragment.setText("这个是群组页面");
break;
default:
break;
}
return view;
}
}