【问题标题】:How to add images to Tabhost如何将图像添加到 Tabhost
【发布时间】:2013-12-12 07:29:20
【问题描述】:

我想为已在选择器 xml 中添加的 Tabhost 添加背景图像。但不确定如何在启动 Tab 时添加资源。下面是标签:

要添加的代码: getResources().getDrawable(R.drawable.tabicon)

/* Tabs */
        Bundle bundle = getIntent().getExtras();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        // First Activity
        intent = new Intent().setClass(this, InfoListView.class);
        spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent);
        tabHost.addTab(spec);

        // Second Activity
        intent = new Intent().setClass(this, LogListView.class);
        spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log").setContent(intent);
        tabHost.addTab(spec);

        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95;
        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95;
        /* Tabs ends */

【问题讨论】:

  • 我已经更新了我的答案,它可以工作

标签: android android-style-tabhost


【解决方案1】:

创建一个TextView,给那个textView设置文本,给它设置背景drawable,然后把这个textview设置为indicator

【讨论】:

  • 例子?如何在 TabHost 中创建 TextView ?
  • 查看下一个答案。 :)
【解决方案2】:

有点像这样:

TextView mTv = new TextView(mContext);
mTv.setText(/*Your-text*/);
mTv.setBackgroundDrawable(mContext.getResources.getDrawable(/*id-of-your-image*/));
spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent);
tabHost.addTab(spec);

【讨论】:

  • 我知道了...谢谢,会尝试
【解决方案3】:

在你的 tabhost 被指示器和其他东西添加后,像这样添加背景

 tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab 
 tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab           

【讨论】:

  • Shakeeb...我正在截屏它是如何来的。它来不及。看起来我们正在设置图标而不是背景图像......这是正确的做法吗?请看我的问题的截图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 2021-11-16
  • 2011-08-07
  • 2011-06-15
相关资源
最近更新 更多