Customizing ActionBar Tabs on Android 4

谁说我不能喝 提交于 2019-11-30 11:28:00

Edit: Use ActionBarCompat in the support library.


I suggest you use Jake Wharton's ActionBarSherlock for two reasons:

  1. ActionBarSherlock is an extension of the compatibility library that facilitates the use of the action bar across all versions of Android. This means users running your application on pre-HoneyComb versions of Android will be able to use the ActionBar for navigation as well. This is preferable to both the user (since the ActionBar is awesome!) and to you (since you don't have to worry about separating the control flow of your application based on whether or not the ActionBar will be available. For instance, without a pre-HoneyComb compatible ActionBar, you might have to worry about creating multiple Activitys for a single screen... one that is optimized for usage with the ActionBar, and one that can be run on pre-HoneyComb devices.

  2. With ActionBarSherlock, styling your ActionBar is easy! I could go into detail on how to do it, but the library already comes with a LOT of sample code that illustrates how to what you are describing above.

Installing/making use of the library is very easy and is almost identical to making use of the normal ActionBar provided in the Android SDK (mostly just call getSupportActionBar() instead of getActionBar()). If you have any questions, let me know.

p.s. Also note that ActionBarSherlock provides all of the functionalities that the ViewPagerExtensions library provides and more. I would definitely recommend using the former over the latter.

There's a brand new tool to generate the correct 9patch images for the actionbar : http://jgilfelt.github.com/android-actionbarstylegenerator/

I have found using style guides for the tabs to very inconsistent for honeycomb, It was much easier to just inflate a custom view that does want you want.

Fragment serivceFrag = new ServicesFragment();
Tab serviceTab = bar.newTab();
TextViewPlus serviceTabTextView =(TextViewPlus)getLayoutInflater().inflate(R.layout.tab_item_layout, null);
serviceTabTextView.setText(servicesLabel);
serviceTab.setCustomView(serviceTabTextView);
serviceTab.setTabListener(new TabListenerFragment(serivceFrag,"Service",R.id.MainInfoPaneFragment));
bar.addTab(serviceTab);

This to me was much simpler,

Change to:

<style name="MyActionBarTabTextStyle" parent="@android:style/Widget.Holo.ActionBar.TabText">
     <item name="android:textSize">14dip</item>
     <item name="android:padding">0dip</item>
</style>

I think you're inheriting from the wrong style, try:

<style name="MyActionBarTabTextStyle" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textSize">14sp</item>
    <item name="android:padding">0dip</item>
</style>

EDIT: also, make sure you use sp instead of dip for font size (14sp)

Marckaraujo

I strongly recommend that you use ViewPagerExtensions library, it give you 5 different styled tabs to you choose from, also the problem that you have to fill tabs in portrait and landscape ,ode for each screen size is already solved in this library.

By the way, if you look inside the library´s code, you can easy theme this yourself.

Also, if you have trouble implementing this with fragment page, look actionbarsherlock-tabs-multi-fragments

You can use the setCustomView method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!