How do I use tabHost for Android

前端 未结 2 2111
清歌不尽
清歌不尽 2020-12-23 10:54

I have looked at posts on Stack Overflow and at tutorials on other websites, and I cannot understand how to use TabHost. Can someone please explain it to me an

2条回答
  •  鱼传尺愫
    2020-12-23 11:20

    First of all while TabHost is not deprecated, TabActivity on other hand is deprecated due to Fragment API.

    There are two ways to use TabHost; using Fragment via FragmentTabHost and using TabHost.TabContentFactory.

    1. Using Fragment via FragmentTabHost

    This sample code show you how to use TabHost in Activity.

    FragmentTabHostActivity.java

    public class FragmentTabHostActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_tab_host_activity);
    
            FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
            fragmentTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
            fragmentTabHost.addTab(getTabSpec1(fragmentTabHost), Tab1Fragment.class, null);
            fragmentTabHost.addTab(getTabSpec2(fragmentTabHost), Tab2Fragment.class, null);
        }
    
        private TabHost.TabSpec getTabSpec1(FragmentTabHost tabHost) {
            return tabHost.newTabSpec("First Tab")
                .setIndicator("Tab1");
        }
    
        private TabHost.TabSpec getTabSpec2(FragmentTabHost tabHost) {
            return tabHost.newTabSpec("Second Tab")
                .setIndicator("Tab 2");
        }
    }
    

    fragment_tab_host_activity.xml

    
    
        
    
            
    
            
    
        
    
    
    

    Actually by using Fragment, you can use Tab inside a Fragment (Android docs).

    2. Using TabHost.ContentFactory

    TabHostActivity.java

    public class TabHostActivity extends AppCompatActivity implements TabHost.TabContentFactory {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
            tabHost.setup();
    
            tabHost.addTab(getTabSpec1(tabHost));
            tabHost.addTab(getTabSpec2(tabHost));
        }
    
        private TabHost.TabSpec getTabSpec1(TabHost tabHost) {
            return tabHost.newTabSpec("First Tab")
                .setIndicator("Tab1")
                .setContent(this);
        }
    
        private TabHost.TabSpec getTabSpec2(TabHost tabHost) {
            return tabHost.newTabSpec("Second Tab")
                .setIndicator("Tab 2")
                .setContent(this);
        }
    
        @Override
        public View createTabContent(String tag) {
            return LayoutInflater.from(this).inflate(R.layout.activity_tab_1, null);
        }
    }
    

    activity_main.xml

    
    
    
        
    
            
    
            
    
        
    
    
    

    However, I personally recommend using newest Material Design style TabLayout class.

提交回复
热议问题