【问题标题】:Need assistance changing the background color of my tab host需要帮助更改我的标签主机的背景颜色
【发布时间】:2013-02-18 07:35:13
【问题描述】:

我一直在尝试更改选项卡主机中的选项卡的背景颜色,但未能成功,我对 java 非常陌生(实际上编码很好)并且对此感到困惑。任何简明的帮助都非常感谢。

活动代码(其中一些)

@SuppressWarnings("unused")
Resources res = getResources();     // Resource object to get Drawables    
TabHost tabHost = getTabHost();     // The activity TabHost   
TabHost.TabSpec spec;                   Intent intent;                      

// Create an Intent to launch an Activity for the tab (to be reused)    
intent = new Intent().setClass(this, jobActivity.class);    

// Initialise a TabSpec for each tab and add it to the TabHost    
spec = tabHost.newTabSpec("job").setIndicator(" Job Details ")                   

        .setContent(intent);    
tabHost.addTab(spec);  

以及相关的 XML 文件

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_color">
<TabHost 
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

    <LinearLayout        
    android:orientation="vertical"        
    android:layout_width="fill_parent"        
    android:layout_height="fill_parent"        
    android:padding="5dp"
    > 
       <HorizontalScrollView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none"
            >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

     </HorizontalScrollView>
            <FrameLayout            
            android:id="@android:id/tabcontent"           
            android:layout_width="wrap_content"       
            android:layout_height="fill_parent" 
            android:padding="5dp"
           >  


         </FrameLayout>
     </LinearLayout>

</TabHost>
</LinearLayout>

这对某人来说应该是一个不错的简单答案;我希望!

在此先感谢

******** 编辑 - 已更新但仍无法正常工作的代码 ************** 现在得到了下面的工作代码 - 但似乎 setTabColors 方法不起作用(甚至不确定因为它被调用,因为从未见过日志的注释)

 public class EPCTabNotesActivity extends TabActivity{


public static final String LOG_TAG = "dbtest";



 public void onCreate(Bundle savedInstanceState) {   
super.onCreate(savedInstanceState);    


setContentView(R.layout.maintab);    


@SuppressWarnings("unused")
Resources res = getResources();     // Resource object to get Drawables    
TabHost tabHost = getTabHost();     // The activity TabHost   

TabHost.TabSpec spec;               
Intent intent;                          



// Create an Intent to launch an Activity for the tab (to be reused)    
intent = new Intent().setClass(this, jobActivity.class);    

// Initialise a TabSpec for each tab and add it to the TabHost    
spec = tabHost.newTabSpec("job").setIndicator(" Job Details ")                   

        .setContent(intent);  

tabHost.addTab(spec);    

// Do the same for the other tabs    

// ..... More tabs

      setTabColors(tabHost);    // need to call setTabColors AFTER all the tabs are added to     thetab spec, else the For loop fails as its empty (therefore = 0)
tabHost.setCurrentTab(0);



}

private void setTabColors(TabHost tabHost) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{   
      tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED); //unselected tab

      Log.v(LOG_TAG,    "tab color " );
}
 }
 }

【问题讨论】:

    标签: java xml colors tabs


    【解决方案1】:

    你可以试试这个:

        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);
    

    i : 指要设置颜色的选项卡。例如:下面的代码片段将改变第一个标签的颜色。

        tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.RED);
    

    将此代码放在活动文件中。

    【讨论】:

    • 将此代码写入您的活动文件中,“i”是指您要更改其颜色的选项卡。因此,如果有 3 个选项卡并且您想更改第一个的颜色,则可以将 i 替换为 0。
    • 感谢有关 i 的回答 - 有道理。但是,我似乎在其静态(或非静态)性质方面存在问题-不会遵守,并且 Eclipse 注释指出“无法从 TabHost 类型对非静态方法 getTabWidget() 进行静态引用”-有什么想法吗?
    • 您是否在静态类中使用它?静态类不能调用非静态方法。您的代码的 sn-p 会有所帮助。您可以参考link了解更多信息
    • OP 随着我的进步而更新 - 我上面关于静态和非静态的问题已解决(我无法正确输入:))
    • 需要将我的方法调用放在不同的地方 0 然后工作 - 上面的代码已编辑
    猜你喜欢
    • 1970-01-01
    • 2022-12-12
    • 2011-03-04
    • 2016-08-20
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多