【问题标题】:SetCurrentTab problems [Android]SetCurrentTab 问题 [Android]
【发布时间】:2011-04-18 12:03:43
【问题描述】:

您好,我收到屏幕旋转的当前选项卡,代码如下:

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


}

结果是我选择了正确的选项卡,但总是有第一个选项卡的内容...(选择的选项卡可以,显示的内容不是)...

我能做什么?

【问题讨论】:

    标签: android tabs android-tabhost


    【解决方案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);
    

    【讨论】:

    • 您好,感谢您的回答。不知道我怎么错过了那个...但这仍然没有解决整个问题... 1. tab 和 2. tab 效果很好... 3. tab 只是文本... 当我转身时屏幕上它在选项卡下方显示空白屏幕(只是白色)
    • 此外,我注意到翻转屏幕会导致在某些时候没有显示数据...这是为什么呢?
    • 在测试时我发现有时它充当intent = new Intent().setClass(this,Currently.class);没有设置...有时还可以...
    • 如果方向更改会造成如此大的麻烦,请删除它。
    • 在您的第三个选项卡中,您的文本可能会落后于选项卡。在布局中应用一些顶部填充。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2011-08-04
    相关资源
    最近更新 更多