3

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

1 回答 1

0

试试这个,它对我有用..在你的主要活动的 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); 

}

您必须按照与主活动相同的程序将选项卡添加到您的子活动...

在子活动中

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);

            }
于 2013-01-31T10:42:05.250 回答