我在我的代码中使用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);
}
答案 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
的每个标签,并访问与标题标签关联的TextView
。 setTextColor
方法用于将文本颜色更改为您想要的颜色(在此示例中为红色)。查看TabWidget的文档可能是值得的。
请特别注意您需要的额外导入语句:import android.widget.TextView
和import 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中Color
(docs)的更多信息,请访问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"));
}