Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用TabHost如何更改文本颜色?_Java_Android_Tabs_Android Tabhost - Fatal编程技术网

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

Java 使用TabHost如何更改文本颜色?,java,android,tabs,android-tabhost,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)

我在代码中使用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);


}

您可以通过选项卡标题
TextView
更改它。详情请参阅:

文本颜色为红色的示例用法如下:

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);
        }
    }
}
如果你有任何错误,请张贴,我会设法解决

有关Android中
Color
()的更多信息,请参见

可以找到一个在线工具,用于查找您要查找的十六进制颜色代码。

TabHost TabHost=getTabHost();

对于(int i=0;iPretty)java新手,我尝试实现它,但恐怕对我来说没有多大意义:/您应该能够从我编辑的答案中的代码中复制/粘贴。如果这不起作用,请告诉我问题所在,我会尽力帮助您。
<?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>
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"));
}