Android Actionbar set width to the tabs
Currently I'm busy on a WHMCS app for my company. As navigation I want to use the Tab functionality in the actionbar.
However, how can you edit the width of a tab? I need 5 together on one screen without the need to scroll. I already tried it with some styles like this:
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="@android:attr/actionBarTabStyle">@style/tab_nav</item>
<item name="android:actionBarTabTextStyle">@style/actionBarTabTextStyle</item>
</style>
<style name="actionBarTabTextStyle" parent="android:Widget.Holo.Light.ActionBar.TabText.Inverse">
<item name="android:padding">1dp</item>
</style>
<style name="tab_nav" parent="android:style/Widget.Holo.Light.Tab">
<item name="android:paddingLeft">-25dp</item>
</style>
<style name="CustomTabTextStyle" parent="@android:style/TextAppearance.Holo">
<item name="android:textColor">#2456c2</item>
</style>
However, the width won't shrimp when using a higher negative value.
嗨,我认为这可能会帮助你...这段代码实际上使操作栏绑定到屏幕宽度,所以标签会自己调整=)
private void setTabsMaxWidth() {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
final ActionBar actionBar = getActionBar();
final View tabView = actionBar.getTabAt(0).getCustomView();
final View tabContainerView = (View) tabView.getParent();
final int tabPadding = tabContainerView.getPaddingLeft() + tabContainerView.getPaddingRight();
final int tabs = actionBar.getTabCount();
for(int i=0 ; i < tabs ; i++) {
View tab = actionBar.getTabAt(i).getCustomView();
TextView text1 = (TextView) tab.findViewById(R.id.text1);
text1.setMaxWidth(screenWidth/tabs-tabPadding-1);
}
}
如果您需要“在一个屏幕上同时显示5个而不需要滚动”,请更好地使用布局顶部的TabHost。
链接地址: http://www.djcxy.com/p/13532.html