Icon in Tab is not showing up

后端 未结 4 849
情书的邮戳
情书的邮戳 2020-11-27 14:03

I\'m starting with Android and when I try to do a TabHost with an icon and a text. Only the text is visible, if I left the text in blank it is possible to see the icon. I wa

4条回答
  •  情深已故
    2020-11-27 14:06

    I try the following way to do it and it works fine:

    1. using setIndicator("TAB") to set text only.
    2. using setBackgroundDrawable(-) to set icon.

    Sample code:

        final TabHost               tabHost = (TabHost)findViewById(android.R.id.tabhost);
        final Resources             res = getResources();
        int i;
    
        tabHost.setup(); //Call setup() before adding tabs if loading TabHost using findViewById(). 
    
        TabHost.TabSpec ts = tabHost.newTabSpec("trackinfo");
        //ts.setIndicator("", res.getDrawable(R.drawable.icon_trackinfo));
        ts.setIndicator("Track Information");
        ts.setContent(R.id.tabTrackInfo);
        tabHost.addTab(ts);
    
        TabHost.TabSpec ts2 = tabHost.newTabSpec("collection");
        //ts2.setIndicator("", res.getDrawable(R.drawable.icon_collection_gray));
        ts2.setIndicator("My Collection");
        ts2.setContent(R.id.tabMyCollecton);
        tabHost.addTab(ts2);
    
        tabHost.setOnTabChangedListener(new OnTabChangeListener(){
            @Override
            public void onTabChanged(String tabId) 
            {
                if("trackinfo".equals(tabId)) {
                    //
                    TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
                    View tabView = tw.getChildTabViewAt(0);
                    TextView tv = (TextView)tabView.findViewById(android.R.id.title);
                    tv.setTextColor(TabColor[0]);
                    tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_selected));
    
                    tabView = tw.getChildTabViewAt(1);
                    tv = (TextView)tabView.findViewById(android.R.id.title);
                    tv.setTextColor(TabColor[1]);
    
                    tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_gray));
                }
                if("collection".equals(tabId)) {
                    //
                    TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
                    View tabView = tw.getChildTabViewAt(0);
                    TextView tv = (TextView)tabView.findViewById(android.R.id.title);
                    tv.setTextColor(TabColor[1]);
                    tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_gray));
    
                    tabView = tw.getChildTabViewAt(1);
                    tv = (TextView)tabView.findViewById(android.R.id.title);
                    tv.setTextColor(TabColor[0]);
                    tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_selected));
    
            }
            }}); // END OF tabHost.setOnTabChangedListener
    
        // After Tabs being added
        // change font settings 
        TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
        for(i=0;i<2;i++)
        {
            View tabView = tw.getChildTabViewAt(i);
            TextView tv = (TextView)tabView.findViewById(android.R.id.title);
            tv.setHeight(25);
            tv.setTextColor(TabColor[i]);
            tv.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC);
            tv.setTextSize(20);      
            tabView.setBackgroundDrawable(res.getDrawable(TabIcon[i]));
    
        }
    

提交回复
热议问题