SetCurrentiTem问题[Android]

时间:2011-04-18 12:03:44

标签: android tabs android-tabhost

您好我在屏幕旋转时收到当前标签,其代码如下:

@Override
public Object onRetainNonConfigurationInstance()
{
    return CurrentTab;
}

CurrentTab设置在(onTabChange)...

之前

在屏幕旋转中,我记录收到的数据:

if (getLastNonConfigurationInstance() != null &&                          
    (Integer)getLastNonConfigurationInstance() >= 0 &&   
    (Integer)getLastNonConfigurationInstance() < 4) 
    {
        CurrentTab = (Integer)getLastNonConfigurationInstance();
        Log.d(" - OOOOK", "Stevilo je: " + CurrentTab.toString());
    }
createView();

在createView中,我有以下代码:

private void createView()
{
        /** TabHost will have Tabs */
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setOnTabChangedListener(this);

        tabHost.clearAllTabs();

        /** TabSpec used to create a new tab.
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */

        /** tid1 is firstTabSpec Id. Its used to access outside. */

        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); 
        TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
        TabSpec forthTabSpec = tabHost.newTabSpec("tid1");

        /** TabSpec setIndicator() is used to set name for the tab. */
        /** TabSpec setContent() is used to set content for a particular tab. */
        firstTabSpec.setIndicator("Kanali", getResources().getDrawable(R.drawable.menu_channels)).setContent(new Intent(this,Channels.class));
        secondTabSpec.setIndicator("Trenutno", getResources().getDrawable(R.drawable.menu_current)).setContent(new Intent(this,Currently.class));
        thirdTabSpec.setIndicator("Opomnik", getResources().getDrawable(R.drawable.menu_reminder)).setContent(new Intent(this,Reminders.class));
        forthTabSpec.setIndicator("O programu", getResources().getDrawable(R.drawable.menu_about)).setContent(new Intent(this,About.class));


        /** Add tabSpec to the TabHost to display. */
        tabHost.addTab(firstTabSpec); 
        tabHost.addTab(secondTabSpec);
        tabHost.addTab(thirdTabSpec);
        tabHost.addTab(forthTabSpec);

        /** applying a theme to app */
        tabHost.setBackgroundColor(Color.WHITE);

        LinearLayout linearTabHost = (LinearLayout) tabHost.getChildAt(0);
        TabWidget tw = (TabWidget) linearTabHost.getChildAt(0);

        /* style and mod tabs */
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            RelativeLayout rllf = (RelativeLayout) tw.getChildAt(i);
            TextView lf = (TextView) rllf.getChildAt(1);
            lf.setTextSize(12);

            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.BLACK);
        }
        tabHost.getTabWidget().getChildAt(CurrentTab).setBackgroundColor(Color.parseColor("#666666"));
        tabHost.getTabWidget().setCurrentTab(CurrentTab);


}

结果是我选择了适当的标签,但总是有第一个标签内容...(选择标签是好的,显示的内容不是)...

我该怎么办?

1 个答案:

答案 0 :(得分:1)

TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); 
        TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
        TabSpec forthTabSpec = tabHost.newTabSpec("tid1");

将上述内容更改为独特的内容。

像这样初始化

Intent intent; 

    intent = new Intent().setClass(this, TabHome.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("alaune").setIndicator("A la Une",null)
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, TabHome.class);

    spec = tabHost.newTabSpec("photos").setIndicator("Photos",null)
                  .setContent(intent);

    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabGallery.class);
    spec = tabHost.newTabSpec("videos").setIndicator("Videos",null)
                  .setContent(intent);

    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabVideos.class);

    spec = tabHost.newTabSpec("journal").setIndicator("Journal",null)
                  .setContent(intent);    
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabJournal.class);
    spec = tabHost.newTabSpec("direct").setIndicator("Direct",null)
                  .setContent(intent);  

    tabHost.addTab(spec);  
    tabHost.setCurrentTab(0);