Newbie: how to change tab font size?

I follow the android, developing a tutorial tab layout to implement a simple tab layout.

Based on this tutorial, I asked myself a question on how to change the font size of a tab

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

 <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="8dip" /> 

but it has no effect.

Can anyone provide the right way to change the font size of a tab?

+4
source share
4 answers

Here you can use 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"); 

Change start

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

Edit endings

 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 can change the color , size and font of the text

+11
source

if you want to achieve this, you must inflate the tab layout.

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

You should put testview instead of the image and define the text size property according to you. Hope this helps.

+5
source

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

Add this first to your oncreate

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

and then inside your creation, create it 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 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 tab attributes in my view and change text and background in oncreate methods

+1
source

I'm not sure if you can change textSize in TabWidget I just check the attributes you can use and there is no way to directly edit the text.

the only way is to inflate your own layout.

0
source

All Articles