开发者

How to add padding to a tabs label?

开发者 https://www.devze.com 2023-02-19 18:09 出处:网络
I just started programming for android. I\'m using a tab based layout in my app. I would like to put some padding around the tab label so that it\'s not so close to the icon.

I just started programming for android. I'm using a tab based layout in my app. I would like to put some padding around the tab label so that it's not so close to the icon.

here is how the label is put in:

in main.java

spec = tabHost.newTabSpec("tab1").setIndicator(this.getString(R.string.tab1), res.getDrawable(R.drawable.ic_tab1)).setContent(intent);

and in string.xml

<string name="tab1">my tab开发者_StackOverflow社区 label</string>

I've been searching and trying to figure this out for several hours now. I could just make the icons smaller in about two minutes but I like their size.

Can anyone suggest the best way to do simple formatting to the tab label?


tabHost.addTab(tabHost.newTabSpec("tab1").setContent(
            new Intent(this, DealCities.class)).setIndicator(prepareTabView("Deals",R.drawable.deal)));

Where prepareTabView is a method.. In these method Inflate a view and add Image and Text as follows :

private View prepareTabView(String text, int resId) {
    View view = LayoutInflater.from(this).inflate(R.layout.tabs, null);
    ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
    TextView tv = (TextView) view.findViewById(R.id.TabTextView);
    iv.setImageResource(resId);
    tv.setText(text);
    return view;
}

Where tabs is the inflated view and its xml as follows :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:id="@+id/TabLayout"  android:background="@drawable/tab_bg_selector"
android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:gravity="center"
padding="5dip">


<ImageView android:id="@+id/TabImageView" android:src="@drawable/icon"
    android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="@+id/TabTextView" android:text="Text"
    android:paddingTop="5dip" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:textColor="@color/black"
    android:textAppearance="@style/TabTextViewStyle" />
</LinearLayout>

Now you can make Your Paddings as you like..


A simpler solution is :

 for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
     tabHost.getTabWidget().getChildAt(i).setPadding(10,10,10,10); 
 }


just use this code

    TabWidget widget = mTabHost.getTabWidget();
    for (int i = 0; i < widget.getChildCount(); i++) {

        //adjust height
        widget.getChildAt(i).getLayoutParams().height =40;

       //adjust textview
        TextView tv = (TextView) widget.getChildAt(i).findViewById(android.R.id.title);
        if (tv == null) {
            continue;
        }           
        tv.setAllCaps(false);
        tv.setTextSize(15);

        //set padding
        tv.setPadding(10,5, 10, 5);

    }
0

精彩评论

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