Android: TabHost without TabActivity

前端 未结 4 1911
旧巷少年郎
旧巷少年郎 2020-11-27 02:56

I want to create tabs without extending TabActivity. (The reason is that TabActivity cannot handle a custom titlebar as it seems). I have

pu         


        
相关标签:
4条回答
  • 2020-11-27 03:19

    Before calling tabHost.setup(mLocalActivityManager); you need to add this line.

    mlam.dispatchCreate(savedInstanceState);
    tabHost.setup(mlam );
    

    similarly, you need to add for onResume,

    mlam.dispatchResume(); 
    

    onPause(),

     mlam.dispatchPause(isFinishing());
    
    0 讨论(0)
  • 2020-11-27 03:19
    public class ScoreboardActivity extends Activity {
        LocalActivityManager mlam;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_scoreboard);
            mlam = new LocalActivityManager(this, false);
            mlam.dispatchCreate(savedInstanceState);
            TabHost th = (TabHost) findViewById(android.R.id.tabhost);
            th.setup(mlam);
            th.addTab(th.newTabSpec("Numpad").setIndicator("Numpad").setContent(R.id.tab1));
            th.addTab(th.newTabSpec("CardCount").setIndicator("CardCount").setContent(R.id.tab2));
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_scoreboard, menu);
            return true;
    
    
        }
        @Override
        protected void onResume(){
            super.onResume();
            mlam.dispatchResume();
        }
    
        @Override
        protected void onPause(){
            super.onPause();
            mlam.dispatchPause(isFinishing());
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 03:28

    Please consider using Views as the contents of your tabs. Not only will this result in less code, less consumed heap space, less consumed stack space, and lower CPU utilization, it will also get you past this problem. Here are two examples showing this technique.

    0 讨论(0)
  • 2020-11-27 03:37

    Design considerations notwithstanding, the following does not work at all, and the API seems to indicate that setContent(Intent i) is valid. This works when the activity extends TabActivity, however, extending Activity and adding setup() call results in an exception at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)

    Funny thing is, the LogCat suggests I forgot to call setup()

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
    
    Intent tab1Intent = new Intent(this, ActivityOne.class);
    Button tab1View = new Button(this);
    tab1View.setText("Activity 1");
      mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator(tab1View).setContent(tab1Intent));
    
    0 讨论(0)
提交回复
热议问题