简体   繁体   中英

android: can't display indicator text and drawable on tabhost

I'm trying to do a simple tab app in android with four tabs tabs. My problem is that when I want to show icon and indicator, it just display only text indicator, i want to display drawable and indicator text on tabhost, this is my code :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        //hide title bar
                BasicDisplaySettings.toggleTaskBar(SimasCardMainActivity.this, false);
                //show status bar
                BasicDisplaySettings.toggleStatusBar(SimasCardMainActivity.this, true);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.simascard);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, TerbaruSimasCard.class);
        spec = tabHost.newTabSpec("Terbaru").setIndicator("Terbaru",
                  res.getDrawable(R.drawable.menu_terbaru))
                  .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, MerchantSimasCard.class);
        spec = tabHost.newTabSpec("Merchant").setIndicator("Merchant",
                  res.getDrawable(R.drawable.menu_merchant))
                  .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, TentangSimasCard.class);
        spec = tabHost.newTabSpec("Tentang").setIndicator("Tentang",
                  res.getDrawable(R.drawable.menu_tentang))
                  .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, FaqSimasCard.class);
        spec = tabHost.newTabSpec("FAQ").setIndicator("FAQ",
                  res.getDrawable(R.drawable.menu_faq))
                  .setContent(intent);
        tabHost.addTab(spec);


        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++){
            tabHost.getTabWidget().getChildAt(i).setPadding(0,0,0,0);
            tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(null);
        }
        tabHost.setCurrentTab(0);
    }

//  @Override
    public void onBackPressed() {
     finish();
    }

when i use

spec = tabHost.newTabSpec("Tentang").setIndicator("",
                      res.getDrawable(R.drawable.menu_tentang))
                      .setContent(intent);

it will show an icon, but when i add text on setIndicator such as setIndicator("Tentang") it show only indicator text on tabhost, i don't know where is something wrong with my code,I've tried to get increase the tab height, but it doesn't work, i hope someone can help me to solve my problem. thank you

Remove this tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(null);

and replace it with

tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.menu_tentang);

i have done it like this and it is working fine

Resources ressources = getResources(); 
    TabHost tabHost = getTabHost();

    Intent addfriend = new Intent().setClass(this, yourclass1.class);
    Intent mailfriend = new Intent().setClass(this, yourclass2.class);
    tabHost.addTab(tabHost.newTabSpec("child1").setIndicator("your tag",getResources().getDrawable(R.drawable.youpng)).setContent(addfriend));
    tabHost.addTab(tabHost.newTabSpec("child1").setIndicator("your tag",getResources().getDrawable(R.drawable.yourpng)).setContent(mailfriend));



    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(getResources().getColor(R.color.anycolor));
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.yourpng);

    tabHost.getTabWidget().getChildAt(1).setBackgroundColor(getResources().getColor(R.color.anycolor));
    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.youpng);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM