Example of using Android tabs with Views instead of Activities?

后端 未结 2 1000
北海茫月
北海茫月 2020-12-07 14:05

The Android Developers TabWidget tutorial says the following:

\"You can implement your tab content in one of two ways: use the tabs to swap Views within the same Act

2条回答
  •  轮回少年
    2020-12-07 14:42

    I think in the .setContent method of each tab you pass in the view you wish to use:

    TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
    spec1.setContent(R.id.AnalogClock01);
    spec1.setIndicator("Analog Clock");
    

    Here's an example I found awhile back:

        
    
    
      
        
        
          
          
        
      
    
    

    And the Java code for this example is as follows:

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TabHost;
    
    public class tabexample extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
    
            tabs.setup();
    
            TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
    
            spec1.setContent(R.id.AnalogClock01);
            spec1.setIndicator("Analog Clock");
    
            tabs.addTab(spec1);
    
            TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
            spec2.setContent(R.id.DigitalClock01);
            spec2.setIndicator("Digital Clock");
    
            tabs.addTab(spec2);
        }
    }
    

提交回复
热议问题