新手上路:如何更改标签的字体大小?字体大小、如何更改、新手、标签

2023-09-06 03:59:54 作者:叼着棒棒糖闯天下

我遵循了Android开发的标签布局教程以实现一个简单的标签布局。

I follow the android develops tutorial of tab layout to implement a simple tab layout.

根据该教程,我得到了我的脑海里的问题,那就是如何更改标签字体大小??

Based on that tutorial, I got an question in my mind, that's how to change the tab font size??

我试图通过添加属性安卓更改标签字体大小:TEXTSIZE =8DIP< TabWidget ...> 布局 XML 文件:

I tried to change the tab font size by adding the attribute android:textSize="8dip" in <TabWidget ...> of the layout xml file :

<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:textSize="8dip" 
/>

,但它并不需要任何效果。

but it does not take any effect.

任何人都可以提供合适的方法来改变标签上的字体大小?

Anyone can provide the right way to change the font size on the tab?

推荐答案

在这里,你走你的tabactivity使用

Here you go in your tabactivity use

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = (TabHost) getTabHost(); // The activity TabHost

TabHost.TabSpec spec; // Resusable TabSpec for each tab
Typeface localTypeface1 = Typeface.createFromAsset(getAssets(),
        "fonts/arial.ttf");

修改启动

tabHost.getTabWidget().setBackgroundDrawable( getResources().getDrawable(R.drawable.bluenavbar));

修改完

TextView txtTab = new TextView(this);
txtTab.setText(getString(R.string.top_news));
txtTab.setPadding(8, 9, 8, 9);
txtTab.setTextColor(Color.WHITE);
txtTab.setTextSize(14);
txtTab.setTypeface(localTypeface1);
txtTab.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
txtTab.setBackgroundResource(R.drawable.tab_news);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("topNews").setIndicator(txtTab).setContent(new Intent(this, TopNewsGroup.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);

现在你就可以改变的颜色 尺寸和字体的文字

Now you'll be able to change the color, size and typeface of the text