8

I have a basic question. In many tabhost examples, we find tabs with image and text.

In my case, I would like to only display a text, but the issue is that my text is horizontally centered but not vertically (The text is at the bottom of my tab).

I tried : android:layout_gravity="center" in the framelayout, but it doesn't work.

Do you have any idea, please ?

My xml.

<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center">

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

        </FrameLayout>

    </LinearLayout>

</TabHost>

solved : I customized my tabs thanks to the following tutorial : http://joshclemm.com/blog/?p=136

Thank you

Miroufle
  • 125
  • 2
  • 10
  • Its better to use a custom tab as default tabs has size of icons and text both. – Lalit Poptani Oct 18 '11 at 11:43
  • possible duplicate of [How to center align text in a tab bar in Android](http://stackoverflow.com/questions/5164443/how-to-center-align-text-in-a-tab-bar-in-android) – user7116 Oct 18 '11 at 13:59
  • The same question was asked a long time ago. Please read this issue [enter link description here](http://stackoverflow.com/questions/5164443/how-to-center-align-text-in-a-tab-bar-in-android) – Dimon Sep 18 '11 at 21:32
  • Yes, I saw this post, but I still have the issue. :( – Miroufle Sep 19 '11 at 18:53
  • 1
    Thank you.you're right. I customized my tabs thanks to the following tutorial : http://joshclemm.com/blog/?p=136 – Miroufle Oct 19 '11 at 21:04

3 Answers3

4
  1. Try to get the tab titles first and set gravity and layouts explicitly as shown below:

    TextView textView = (TextView)tabHost.getTabWidget().getChildAt(0)
    .findViewById(android.R.id.title);
    textView.setGravity(Gravity.CENTER);        
    textView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
    textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
    

    Hope this help.

Community
  • 1
  • 1
udai
  • 3,723
  • 4
  • 16
  • 20
0

Use the below to code to make tab text in center:

RelativeLayout.LayoutParams param=new

RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    param.addRule(RelativeLayout.CENTER_IN_PARENT);

TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0)
            .findViewById(android.R.id.title); // Unselected Tabs
    tv.setTextColor(Color.parseColor("#505050"));
    tv.setTextSize(10);
    tv.setLayoutParams(param); 
Manmohan Pal
  • 1,577
  • 1
  • 12
  • 13
0
android:layout_width="fill_parent"   
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal"
user634545
  • 9,099
  • 5
  • 29
  • 40
  • I tried your solution but it dosen't work. My text still is at the bottom of my tab. Does it work for you ? – Miroufle Sep 18 '11 at 17:04