我一直在尝试并且未能在标签主机中更改我的标签的背景颜色,我对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 " );
}
}
}
答案 0 :(得分:0)
你可以试试这个:
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);
i:指要设置颜色的选项卡。例如:下面的代码段将更改第一个标签的颜色。
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.RED);
将此代码放在活动文件中。