Java 需要帮助更改我的选项卡主机的背景色吗
我一直在尝试在一个选项卡主机中更改选项卡的背景颜色,但失败了。我对java非常陌生(一般来说,我的代码编写得很好),对此我感到困惑。任何简明的帮助都非常感谢 活动代码(以及其中的一些) 以及关联的XML文件Java 需要帮助更改我的选项卡主机的背景色吗,java,xml,colors,tabs,Java,Xml,Colors,Tabs,我一直在尝试在一个选项卡主机中更改选项卡的背景颜色,但失败了。我对java非常陌生(一般来说,我的代码编写得很好),对此我感到困惑。任何简明的帮助都非常感谢 活动代码(以及其中的一些) 以及关联的XML文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="f
<?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>
对某人来说,这应该是一个简单的回答;我希望如此
非常感谢
*********编辑-已更新但仍无法工作的代码***************
现在我们已经得到了下面的工作代码——但是setTabColor方法似乎不起作用(甚至不确定,因为它被调用了,因为日志的注释从未出现过)
公共类EPCTabNotesActivity扩展了TabActivity{
公共静态最终字符串日志\u TAG=“dbtest”;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.maintab);
@抑制警告(“未使用”)
Resources res=getResources();//获取可绘图项的资源对象
TabHost TabHost=getTabHost();//活动TabHost
TabHost.TabSpec;
意图;
//创建为选项卡启动活动的意图(要重用)
intent=new intent().setClass(这是jobActivity.class);
//为每个选项卡初始化TabSpec并将其添加到TabHost
spec=tabHost.newTabSpec(“作业”).setIndicator(“作业详细信息”)
.setContent(意图);
tabHost.addTab(spec);
//对其他选项卡执行相同的操作
//……更多标签
setTabColors(tabHost);//在将所有选项卡添加到选项卡规范后,需要调用setTabColors,否则For循环将失败,因为它是空的(因此=0)
tabHost.setCurrentTab(0);
}
私有void setTabColors(TabHost TabHost){
//TODO自动生成的方法存根
对于(int i=0;i您可以尝试以下方法:
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);
i:指要设置颜色的页签,例如:下面的代码段会改变第一个页签的颜色
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.RED);
将此代码放在活动文件中。将此代码写在活动文件中,“i”指的是要更改其颜色的选项卡。因此,如果有3个选项卡,您首先要更改其颜色,可以将i替换为0。感谢您对i的回答-有意义。但是,我似乎对其静态属性有问题(或非静态)性质-将不兼容,eclipse notes声明“无法从类型TabHost静态引用非静态方法getTabWidget()”-有什么想法吗?你是在静态类中使用它吗?静态类不能调用非静态方法。你的代码片段可能会有所帮助。你可以参考更多信息随着我的进度更新-我上面关于静态和非静态的问题已解决(我无法正确键入:))需要将我的方法调用放在不同的位置0然后工作-上面的代码已编辑
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.RED);