使用TabHost如何更改文本颜色?

时间:2015-03-03 14:41:36

标签: java android tabs android-tabhost

我在我的代码中使用TabHost,我想知道如何更改文本颜色?我认为这将与XML有关,但在查看它时,我不认为它是:

public class HealthyEating extends TabActivity{
Resources res;
TabHost tabHost;
Intent intent;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_healthy_eating);

    res = getResources();
    tabHost = getTabHost();
    TabHost.TabSpec spec;

    intent = new Intent().setClass(this, BreakfastRecipe.class);
    spec = tabHost.newTabSpec("Breakfast Recipes").setIndicator("Breakfast Recipes")
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, LunchRecipe.class);
    spec = tabHost.newTabSpec("Lunch Recipes").setIndicator("Lunch Recipes")
            .setContent(intent);
    tabHost.addTab(spec);
    tabHost.setCurrentTab(0);


}

2 个答案:

答案 0 :(得分:0)

您可以通过标题标题TextView进行更改。详情请见:

Android: Change Tab Text Color Programmatically

文字颜色为红色的示例用法为:

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.myproject.R;

public class HealthyEating extends TabActivity {
    Resources res;
    TabHost tabHost;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_healthy_eating);

        res = getResources();
        tabHost = getTabHost();
        TabHost.TabSpec spec;

        intent = new Intent().setClass(this, BreakfastRecipe.class);
        spec = tabHost.newTabSpec("Breakfast Recipes").setIndicator("Breakfast Recipes")
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, LunchRecipe.class);
        spec = tabHost.newTabSpec("Lunch Recipes").setIndicator("Lunch Recipes")
                .setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(0);

        int titleColor = Color.RED; //<-- change this to the color you want the title text to be
        for(int i = 0;i < tabHost.getTabWidget().getChildCount(); i++)
        {
            TextView textView = (TextView)tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            textView.setTextColor(titleColor);
        }
    }
}

for循环遍历您已添加到TabHost的每个标签,并访问与标题标签关联的TextViewsetTextColor方法用于将文本颜色更改为您想要的颜色(在此示例中为红色)。查看TabWidget的文档可能是值得的。

请特别注意您需要的额外导入语句:import android.widget.TextViewimport android.graphics.Color

此示例适用于以下activity_healthy_eating文件:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost">

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_marginBottom="5dip">
        </TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
        </FrameLayout>
    </LinearLayout>
</TabHost>

如果您收到任何错误,请发布,我会尝试解决。

有关Android中Colordocs)的更多信息,请访问here

用于查找您之后的十六进制颜色代码的在线工具可以找到here

答案 1 :(得分:0)

TabHost tabhost = getTabHost();
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
{
    TextView tv = (TextView) tabhost.getTabWidget().getChildAt    (i).findViewById(android.R.id.title);
    tv.setTextColor(Color.parseColor("#000000"));
}