新手:如何更改标签字体大小?

Lee*_*eem 4 android android-emulator android-layout

我按照android开发教程的标签布局来实现一个简单的标签布局.

基于该教程,我在脑海中得到了一个问题,那就是如何更改选项卡字体大小?

我试图通过添加属性来更改标签的字体大小android:textSize="8dip"<TabWidget ...>布局的XML文件:

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

    android:textSize="8dip" 
/>
Run Code Online (Sandbox Code Playgroud)

但它没有任何效果.

任何人都可以提供更改选项卡上字体大小的正确方法吗?

Rah*_*rma 11

在这里,您可以使用tabactivity

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");
Run Code Online (Sandbox Code Playgroud)

编辑开始

tabHost.getTabWidget().setBackgroundDrawable( getResources().getDrawable(R.drawable.bluenavbar));
Run Code Online (Sandbox Code Playgroud)

编辑结束

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);
Run Code Online (Sandbox Code Playgroud)

现在,您将能够更改文本的颜色,大小字体


Dyn*_*ind 5

如果你想实现它,你应该膨胀标签的布局.

    tabHost = getTabHost(); // The activity TabHost
    tabHost.setOnTabChangedListener(this);
    Resources res = getResources(); // Resource object to get Drawables
    tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Reusable TabSpec for each tab

    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
    TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
    TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");
    TabSpec fifthTabSpec = tabHost.newTabSpec("tid5");

    viewCache[0] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[1] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[2] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[3] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[4] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);

    firstTabSpec.setIndicator(viewCache[0]);
    secondTabSpec.setIndicator(viewCache[1]);
    thirdTabSpec.setIndicator(viewCache[2]);
    fourthTabSpec.setIndicator(viewCache[3]);
    fifthTabSpec.setIndicator(viewCache[4]);

    firstTabSpec.setContent(new Intent(this, HomeTabActivityGroup.class));
    secondTabSpec
            .setContent(new Intent(this, ProfileTabActivityGroup.class));
    thirdTabSpec.setContent(new Intent(this,
            NotificationTabActivityGroup.class));
    fourthTabSpec.setContent(new Intent(this,
            FavoritesTabActivityGroup.class));
    fifthTabSpec
            .setContent(new Intent(this, MoreTabActivityGroupNew.class));

    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(thirdTabSpec);
    tabHost.addTab(fourthTabSpec);
    tabHost.addTab(fifthTabSpec);

    currentTabvalue = tabHost.getCurrentTab();
    C2DMessaging.register(TennisAppActivity.mContext,
            "racquetester@gmail.com");
    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {

        // tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
        switch (i) {
        case 0:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.home);
            break;
        case 1:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.profile);
            break;
        case 2:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.notifications);
            break;
        case 3:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.fav);
            break;
        case 4:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.more);
            break;
        }
    }
Run Code Online (Sandbox Code Playgroud)

// **************************************

这是tab1.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/LinearLayout01" 
 android:layout_width="wrap_content" android:layout_height="wrap_content"
  xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center">
 <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content"  android:layout_height="50dip"></ImageView>
   </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

您应该根据您的情况将testview放在图像视图和settest文本大小属性的位置.我希望这是有帮助的.