Different sized tabs in Android
I have an application with 4 tabs. By default every tab width is 1/4 of the screen width. How can I over开发者_开发知识库ride this?
I need the tabs to have a different width for each one. Any ideas on how to accomplish that?
Try this:
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 35;
Finally I managed to have a different sized tabs using ToggleButton.
My XML is like this
<LinearLayout
android:id="@+id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/header"
>
<ToggleButton
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/icon_home"
android:textOn=""
android:textOff=""
android:background="@drawable/tabselector"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:layout_weight="0"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/top_menubar_separator" />
<ToggleButton
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/icon_store"
android:textOn="Store"
android:textOff="Store"
android:background="@drawable/tabselector"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:layout_weight="2"
/>
Where each ToggleButton is a tab. The trick here is using the weight to expand the tabs to completely fill the screen.
The teab_selector.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/top_menubar_selected"
android:state_pressed="true"
/>
<item android:drawable="@drawable/top_menubar_selected"
android:state_checked="true"
/>
<item android:drawable="@drawable/top_menubar_1px" />
</selector>
I still need to code the logic for un-press a button when a new one is selected and launch an intent with ta activity, but looks nice so far.
Try this:
<TabWidget android:layout_width="wrap_content" ... />
For the sake of someone still using FragmentTabHost and TabWidget, getting the tab to wrap to the text and not use a within-word break strategy can be achieved using a PreDrawListener
on the ViewTreeObserver
of the tab indicator view before setting the text in it. Something like this
final View newTabView = LayoutInflater.from(this).inflate(R.layout.tab_view, null);
mTabHost.addTab(mTabHost.newTabSpec(index + "").setIndicator(newTabView),
YourClassArgument.class, null);
final TextView titleTextView = newTabView.findViewById(R.id.tabTextView);
titleTextView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
titleTextView.getViewTreeObserver().removeOnPreDrawListener(this);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) newTabView.getLayoutParams();
layoutParams.width = (int) Math.ceil(titleTextView.getLayout().getLineWidth(0))
+ newTabView.getPaddingLeft() + newTabView.getPaddingRight();
newTabView.setLayoutParams(layoutParams);
return false;
}
});
titleTextView.setText(tabTitle);
精彩评论