android - 如何在android中的子 Activity 中添加tabHost?

标签 android

我想在 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);
    }
}

最佳答案

试试这个,它对我有用.. 在你的主要 Activity 的 onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    etSearch = (EditText)findViewById(R.id.etSearch);

    TabHost tabhost = getTabHost();

    TabSpec first = tabhost.newTabSpec("All Submissions");        
    first.setIndicator("All Submissions"     ,getResources().getDrawable(R.drawable.submission));
    Intent intent1 = new Intent(this, AllSubmissionActivity.class);
    first.setContent(intent1);
    //tabhost.addTab(first);

    TabSpec second = tabhost.newTabSpec("All Districts");        
    second.setIndicator("All Districts"  ,getResources().getDrawable(R.drawable.district));
    Intent intent2 = new Intent(this, AllDistrictActivity.class);
    second.setContent(intent2);
    //tabhost.addTab(second);

    TabSpec third = tabhost.newTabSpec("All Schools");
    third.setIndicator("All Schools" ,getResources().getDrawable(R.drawable.school));
    Intent intent3 = new Intent(this, AllSchoolActivity.class);
    third.setContent(intent3);
    //tabhost.addTab(third);  

    if(DataContainer.UserCredentials.uname.equals("sadmin"))
    {
        tabhost.addTab(first);
        tabhost.addTab(second);
        tabhost.addTab(third); 
    }
    else if(DataContainer.UserCredentials.uname.equals("b"))
    {
        tabhost.addTab(second);
        tabhost.addTab(third); 
    }
    else
    {
        tabhost.addTab(third); 
    }


    tabhost.setCurrentTab(0); 

}

您必须按照与在主 Activity 中相同的步骤将选项卡添加到您的子 Activity 中...

在子 Activity 中

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.allsubmission);

    initializeTabHost();

}

这是 initializeTabHoast() 方法..

public void initializeTabHost(){



    tabhost = getTabHost();
    TabSpec first = tabhost.newTabSpec("Details"); 

    first.setIndicator("Details" ,null);
    Intent intent1 = new Intent(this, DetailsCrimeOnLink.class);

    intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


    first.setContent(intent1);
    tabhost.addTab(first);

    TabSpec second = tabhost.newTabSpec("Maps");        
    second.setIndicator("Maps" ,null);
    Intent intent2 = new Intent(this, MapsCrimeOnLink.class);
    second.setContent(intent2);
    tabhost.addTab(second);

    TabSpec third = tabhost.newTabSpec("Notes");
    third.setIndicator("Notes" ,null);
    Intent intent3 = new Intent(this, NotesCrimeOnLink.class);
    third.setContent(intent3);
    tabhost.addTab(third); 

    TabSpec fourth = tabhost.newTabSpec("History");
    fourth.setIndicator("History" ,null);
    Intent intent4 = new Intent(this, HistoryCrimOnLink.class);
    fourth.setContent(intent4);
    tabhost.addTab(fourth);  

    tabhost.setCurrentTab(0);

            }

关于android - 如何在android中的子 Activity 中添加tabHost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14621845/

相关文章:

javascript - Android jQuery fadeIn 奇怪的行为

java - 具有相同 Fragment 的 Android 选项卡

Android SearchView X 标记图标

Android 在隐藏后在顶部显示 Activity 标题/状态栏

java - 像在 jQuery ajax 中一样使用 Retrofit 发出 POST Json 请求

java - Android框架中AsyncTask的优缺点是什么?

android - 如何在Android上实现网站过滤器?

c# - 不要破坏负载

android - 存储和解析来自互联网的 XML

android - 如何知道图像是来自相机还是文件系统