我们可以为标签设置背景图片吗?

neh*_*eha 10 tabs android background

我们可以将背景图像设置为这样的标签吗?

替代文字

neh*_*eha 18

以下代码为我工作:

 tabHost.getTabWidget().setBackgroundResource(R.drawable.tabhost_bg);
Run Code Online (Sandbox Code Playgroud)

  • 您需要将具有透明背景(或无任何)的视图设置为选项卡的指示器,以使此解决方案有效. (2认同)

Pin*_*nki 10

试试这个

             getTabHost().addTab(getTabHost().newTabSpec("A")
                    //set the tab name
                    .setIndicator("A")
                    //Add additional flags to the intent (or with existing flags value).
                    .setContent(new Intent(this, A.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));      

            //for creating tab we need to pass tabSpec and tab name to setIndicator and pass intent to its 
                //setContent  to Tab Activity predefined method getTabHost then it will create tab
             getTabHost().addTab(getTabHost().newTabSpec("B")
                    //set the tab name
                        .setIndicator("B")

                         //Add additional flags to the intent (or with existing flags value).
                       .setContent(new Intent(this,B.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) );

getTabHost().addTab(getTabHost().newTabSpec("C")
                    //set the tab name
                        .setIndicator("C")

                         //Add additional flags to the intent (or with existing flags value).
                       .setContent(new Intent(this,C.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) );    


             getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.a);
 getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.b);
 getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.c);
Run Code Online (Sandbox Code Playgroud)