繁体   English   中英

Android:无法在Tabhost上显示指标文字和可绘制内容

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

我正在尝试用四个选项卡选项卡在android中做一个简单的选项卡应用程序。 我的问题是,当我想显示图标和指示器时,它仅显示文本指示器,我想在tabhost上显示可绘制和指示器文本,这是我的代码:

@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();
    }

当我使用

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

它会显示一个图标,但是当我在setIndicatorsetIndicator诸如setIndicator("Tentang")之类的文本时,它仅在tabhost上显示指示符文本,我不知道我的代码在哪里出问题,我试图增加标签高度,但是不起作用,我希望有人可以帮助我解决问题。 谢谢

删除此tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(null);

并替换为

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

我已经做到了,它工作正常

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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