Looking for a universal TabHost style that will work on Android, HTC Sense, Samsung, etc. skins

前端 未结 2 1527
礼貌的吻别
礼貌的吻别 2021-01-02 12:12

The default style for the Android TabHost works fine for straight Android systems. However, on HTC Sense, they use dark text on a dark background, which is unreadable.

2条回答
  •  被撕碎了的回忆
    2021-01-02 12:42

    I'll give you the advise to get completely independent from the TabWidget and create your own Navigation, which you can customize however you want and don't bother with the stiff TabWidget. I don't mean to throw away the awesome TabHost, because it's easy to use the TabHost with a custom navigation:

    First set your TabWidget as gone:

    
        
            
    

    And then create you own navigation in place of it. You could also create a menu (with the hardware menu button) or something like this:

    
        
            
            
            
            
            
                
                
                
                    
                        
                            
                            
                                .....etc....your tabs......
                        
                    
                
            
        
    
    

    What you have to do in code now:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tab_layout);
    
        setTabs();
    
        RadioGroup tabs = (RadioGroup) findViewById(R.id.custom_tabs);
    
        tabs.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.tab_overview:
                    getTabHost().setCurrentTab(0);
                    break;
                case R.id.tab_news:
                    getTabHost().setCurrentTab(1);
                    break;
                ....etc.....
                }
            }
        });
    }
    
    /**
     * Delegate tab creation and adding.
     */
    private void setTabs() {
        // add the necessary tabs
        addTab(R.string.tab_overv_tag, OverviewActivityGroup.class);
        addTab(R.string.tab_news_tag, NewsActivityGroup.class);
                .....etc.....
    }
    
    /**
     * Create a tab with an Activity and add it to the TabHost
     *  
     * @param tagId
     *            resource id of the string representing the tag for finding the tab    
     * @param activity
     *            the activity to be added
     */
    private void addTab(int tagId, Class activity) {
        // create an Intent to launch an Activity for the tab (to be reused)
        Intent intent = new Intent().setClass(this, activity);
        // initialize a TabSpec for each tab and add it to the TabHost
        TabHost.TabSpec spec = usedTabHost.newTabSpec(getString(tagId));
        // use layout inflater to get a view of the tab to be added
        View tabIndicator = getLayoutInflater().inflate(R.layout.tab_indicator, getTabWidget(), false);
        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        usedTabHost.addTab(spec);
    }
    

    The whole indicator stuff is not needed btw.^^ In your ActivityGroups you have to set the appropriate Activities, etc. You know the stuff.

    You can use anything for your navigation and can still use the advantages of a TabHost. Just don't bother with that TabWidget anymore. ;)

提交回复
热议问题