如何更改android中的tabhost的字体大小

如何改变标签的字体大小? 我为选项卡扩展TabActivity。

你可以定义主题,使用样式来实现这一点:

首先,在res/values/styles.xml为您的Activity创build主题(名称: CustomTheme ):

 <style name="CustomTheme" parent="@android:style/Theme"> <item name="android:tabWidgetStyle">@style/CustomTabWidget</item> </style> <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget"> <item name="android:textAppearance">@style/CustomTabWidgetText</item> </style> <style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget"> <item name="android:textSize">20sp</item> <item name="android:textStyle">bold</item> </style> 

然后在你的androidManifest.xml为你的TabActivity或者包含你的TabWidget Activity指定上面的主题:

 <activity android:name="MyTabActivity" android:theme="@style/CustomTheme"> 

这将为您提供您想要的输出(当然,您应该根据自己的喜好更改大小和样式)。

它不漂亮,但尝试这个肮脏的修复:

 TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs); View tabView = tw.getChildTabViewAt(0); TextView tv = (TextView)tabView.findViewById(android.R.id.title); tv.setTextSize(20); 

要么

  //Do this to hack font size of title text LinearLayout ll = (LinearLayout) tabHost.getChildAt(0); TabWidget tw = (TabWidget) ll.getChildAt(0); // for changing the text size of first tab RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0); TextView lf = (TextView) rllf.getChildAt(1); lf.setTextSize(21); lf.setPadding(0, 0, 0, 6); 

略微泛化:

 final TabWidget tw = (TabWidget)mTabHost.findViewById(android.R.id.tabs); for (int i = 0; i < tw.getChildCount(); ++i) { final View tabView = tw.getChildTabViewAt(i); final TextView tv = (TextView)tabView.findViewById(android.R.id.title); tv.setTextSize(20); } 

我在我的代码中使用这段代码,但它只对第一个选项卡的效果,其他3个选项卡仍然没有改变。

  TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs); View tabView = tw.getChildTabViewAt(0); TextView tv = (TextView)tabView.findViewById(android.R.id.title); tv.setTextSize(10);