Newbie: How to change tab font size?

微笑、不失礼 提交于 2019-12-21 17:29:15

问题


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??

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?


回答1:


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");

Edit Starts

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

Edit Ends

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




回答2:


if you want to achieve it you should inflate layout of tab.

    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;
        }
    }

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

This is the 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>

Your should put a testview in the place of image view and settest text size property according to you. I hope this is help.




回答3:


Here is my 2 cents and it works for me...

Add this first inside your oncreate

TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab
    MyView view = null;

then also inside your on create use this for each tab...

intent = new Intent().setClass(this, BrownieTab.class);
    view = new MyView(this, R.drawable.ic_tab_brownie_2, R.drawable.ic_tab_brownie_2, "Yourself");
    view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground));
    spec = tabHost.newTabSpec("inprogress").setIndicator(view).setContent(intent);
    tabHost.addTab(spec);

Finally here is the MyView...

private class MyView extends LinearLayout {
    ImageView iv;
    TextView tv;

    public MyView(Context c, int drawable, int drawableselec, String label) {
        super(c);
        iv = new ImageView(c);
        StateListDrawable listDrawable = new StateListDrawable();
        listDrawable.addState(SELECTED_STATE_SET, this.getResources()
                .getDrawable(drawableselec));
        listDrawable.addState(ENABLED_STATE_SET, this.getResources()
                .getDrawable(drawable));
        iv.setImageDrawable(listDrawable);
        iv.setBackgroundColor(Color.TRANSPARENT);
        iv.setLayoutParams(new LayoutParams(48, 48, (float) 0.0));
        setGravity(Gravity.CENTER);
        tv = new TextView(c);
        tv.setText(label);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundColor(Color.TRANSPARENT);
        tv.setTextColor(Color.BLACK);
        tv.setTextSize(12);

        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, (float) 1.0));
        setOrientation(LinearLayout.VERTICAL);
        addView(iv);
        addView(tv);
        setBackgroundDrawable(this.getResources().getDrawable(
                R.layout.selecttabbackground));
    }
}   

you can change all the attributes about the tabs in the my view and change the text and background in the oncreate methods




回答4:


I'm not shure you can change the textSize in a TabWidget I just check for the attributes you can use and there is no possibility to edit your text directly.

the only way is to inflate your own layout.



来源:https://stackoverflow.com/questions/6532170/newbie-how-to-change-tab-font-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!