【问题标题】:Can we set a background image to tabs?我们可以为标签设置背景图像吗?
【发布时间】:2011-01-21 07:37:14
【问题描述】:

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

【问题讨论】:

    标签: android background tabs


    【解决方案1】:

    以下代码对我有用:

     tabHost.getTabWidget().setBackgroundResource(R.drawable.tabhost_bg);
    

    【讨论】:

    • 在图片选项卡中似乎是透明的......你是怎么做到的?我设法设置了背景,但我的标签在顶部以灰色可见...... :(
    • 您需要将具有透明背景(或无任何背景)的视图设置为选项卡的指示器,以使此解决方案发挥作用。
    【解决方案2】:

    试试这个

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

    【讨论】:

      【解决方案3】:
      TabSpec generalTab = mTabHost.newTabSpec("general");
      generalTab.setIndicator("General", getResources().getDrawable(android.R.drawable.ic_menu_preferences)).setContent(R.id.tabGeneral);
      

      我使用了默认的 android drawable 你可以使用你想要的

      用于设置tabhost的背景

      this.mTabHost = (TabHost)this.findViewById(R.id.tabHost);
          this.mTabHost.setBackgroundResource(R.drawable.back);
      

      【讨论】:

      • 这会将图像设置为各个选项卡的背景。我想将背景图像设置为所有选项卡的背景 [选项卡下方的部分]。
      • This' 将背景图像设置为整个屏幕,而不仅仅是选项卡的背景。
      • 实际上你想要的是以这种方式完成的,它只会将图像设置为整个背景,但是当你将不同的图像放在不同的选项卡中时,这个图像会被选项卡图像隐藏这是我知道的唯一解决方法希望其他人对此有更好的答案
      猜你喜欢
      • 2017-07-31
      • 2013-08-02
      • 2012-02-13
      • 2014-05-31
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      相关资源
      最近更新 更多