Why the tab widget is above the content in android?

前端 未结 3 1901
梦谈多话
梦谈多话 2020-12-07 05:48

Currently I work on a tabhost layout.

In most android app the layout is:

tab1 | tab 2
____________

Tab 1 content 
(if I press on tab1)
3条回答
  •  无人及你
    2020-12-07 06:29

    Try this way,hope this will help you to solve your problem.

    activity_main.xml

    
    
        
    
            
    
            
    
            
    
        
    
    

    tab1_view.xml

    
    
        
    
            
    
                
    
                
            
        
    
    
    

    tab2_view.xml

    
    
        
    
            
    
                
    
                
            
        
    
    
    

    tab3_view.xml

    
    
        
    
            
    
                
    
                
            
        
    
    
    

    MainActivity.java

    public class MainActivity extends FragmentActivity {
    
        private FragmentTabHost mTabHost;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    
            mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1",getResources().getDrawable(R.drawable.ic_launcher)),
                    Home.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2",getResources().getDrawable(R.drawable.ic_launcher)),
                    CarPark.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3", getResources().getDrawable(R.drawable.ic_launcher)),
                    Shop.class, null);
        }
    }
    

    Home.java

    public class Home extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.tab1_view, container, false);
            return V;
        }
    }
    

    CarPark.java

    public class CarPark extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.tab2_view, container, false);
            return V;
        }
    }
    

    Shop.java

    public class Shop extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.tab3_view, container, false);
            return V;
        }
    }
    

提交回复
热议问题