我想在 SubActivity 中添加 TabHost。
例如,我有一个名为 MainActivity 的 Activity,它代表一个 TabHost。我的 tabHost 中有五个选项卡。在每个 tabHost 上,我打开 Activity A、B、C、D、E。
Now when I want move from Activity A to some different activity e.g Z.
then I am not able to add TabHost into Z. It's totally disappeared from Activity Z.
So Is there any solution for this issue ? Please help me.
这是 MainActivity 源:
public class MainActivity extends TabActivity {
public static final String TAG_1 = "tab1";
public static final String TAG_2 = "tab2";
public static final String TAG_3 = "tab3";
public static final String TAG_4 = "tab4";
public static final String TAG_5 = "tab5";
public TabHost mTabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = getTabHost();
setTabs();
}
public void setTabs() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
addTab("Home", TAG_1, createTabDrawable(R.drawable.home), FeedBack.class);
addTab("Near Me", TAG_2, createTabDrawable(R.drawable.search), NearMe.class);
addTab("Share", TAG_3, createTabDrawable(R.drawable.star),Home.class);
addTab("FeedBack", TAG_4, createTabDrawable(R.drawable.settings),FeedBack.class);
addTab("Options", TAG_5, createTabDrawable(R.drawable.settings),NearMe.class);
}
public Drawable createTabDrawable(int resId) {
Resources res = getResources();
StateListDrawable states = new StateListDrawable();
final Options options = new Options();
options.inPreferredConfig = Config.ARGB_8888;
Bitmap icon = BitmapFactory.decodeResource(res, resId, options);
Bitmap unselected = TabBitmap.createUnselectedBitmap(res, icon);
Bitmap selected = TabBitmap.createSelectedBitmap(res, icon);
icon.recycle();
states.addState(new int[] { android.R.attr.state_selected }, new BitmapDrawable(res, selected));
states.addState(new int[] { android.R.attr.state_enabled }, new BitmapDrawable(res, unselected));
return states;
}
public View createTabIndicator(String label, Drawable drawable) {
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);
TextView txtTitle = (TextView) tabIndicator.findViewById(R.id.text_view_tab_title);
txtTitle.setText(label);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) txtTitle.getLayoutParams();
txtTitle.setLayoutParams(params);
ImageView imgIcon = (ImageView) tabIndicator.findViewById(R.id.image_view_tab_icon);
imgIcon.setImageDrawable(drawable);
return tabIndicator;
}
public void addTab(String label, String tag, Drawable drawable, Class<?> c) {
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
spec.setIndicator(createTabIndicator(label, drawable));
spec.setContent(intent);
mTabHost.addTab(spec);
}
}