如何将图像添加到Tabhost

时间:2013-12-12 07:29:21

标签: android android-style-tabhost

我想为Tabhost添加背景图片,我已经在选择器xml中添加了它。但不确定如何在启动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 */

3 个答案:

答案 0 :(得分:0)

创建TextView,将文本设置为该textView,将背景设置为可绘制,并将此textview设置为indicator

答案 1 :(得分:0)

有点像这样:

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

答案 2 :(得分:0)

使用指标初始化tabhost之后,其他内容就像这样添加背景

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