开发者

How to center text in tabhost?

开发者 https://www.devze.com 2023-04-05 21:21 出处:网络
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 ve

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 custo开发者_如何学编程mized my tabs thanks to the following tutorial : http://joshclemm.com/blog/?p=136

Thank you


  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.


android:layout_width="fill_parent"   
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal"


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

精彩评论

暂无评论...
验证码 换一张
取 消