2

I've got a nice ActionBarSherlock setup with three tabs. The left and right tabs are set up just as icons, and the middle one is a custom layout.

In portrait mode, this looks great. The tabs are nicely centered in the action bar, and all is well with the world: enter image description here

However, in landscape mode, the tabs are flushed to the left: enter image description here

Here's how I set up my tabs:

Tab leftTab = mActionBar.newTab().setIcon(R.drawable.ic_action_login_black).setTabListener(tabListener);
mActionBar.addTab(leftTab);

Tab centerTab = mActionBar.newTab().setCustomView(R.layout.actionbar_logo).setTabListener(tabListener);
centerView = (RelativeLayout) centerTab.getCustomView();
mActionBar.addTab(centerTab, true);

Tab rightTab = mActionBar.newTab().setIcon(R.drawable.ic_action_video_black).setTabListener(tabListener);
mActionBar.addTab(rightTab);

And here's my XML for that custom view in the center tab:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false" >

    <ImageView
        android:id="@+id/my_logo"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/my_logo" />

</RelativeLayout>

If it matters, I'm using minSdkVersion=15.

I'd like the tabs to be centered, just as they are in portrait mode. Is that possible?

spitzanator
  • 1,877
  • 4
  • 19
  • 29
  • You should create a layout in the /layout folder and in /layout-land with the same name but diferent attributes ? like "fitXY" if it is an ImageView ... etc and use margin and padding carefully – An-droid Sep 05 '13 at 15:06
  • But how do I control the layout of the whole action bar? As far as I can tell, I only have control of the layouts of the individual tabs. – spitzanator Sep 05 '13 at 20:25
  • Yes you are right, you only can do a layout for the tab – An-droid Sep 06 '13 at 07:15
  • You can ass a custom layout in the actionbar and have all the views centered that looks like tabs. To make your view look like tabs you can use the styles from ABS. – Varun Sep 09 '13 at 19:36
  • That doesn't properly handle the tab-selected state (see the green indicator to show which tab I'd like to use). In the action bar with tabs, that indicator slides nicely among them. I wouldn't be able to get that with custom views. – spitzanator Sep 09 '13 at 22:59
  • can you share your xml code? – RobinHood Sep 13 '13 at 05:23
  • Similar question with a hacky "solution": http://stackoverflow.com/questions/10922845/android-tabs-dont-fill-parent-with-holo-theme – Geobits Sep 13 '13 at 13:37
  • What if you make the `layout_height` equal to `wrap_content`? – Cat Sep 16 '13 at 04:46
  • @Geobits I suppose, for the better, that hacky solution didn't work for me. – spitzanator Sep 16 '13 at 11:18
  • @Eric no dice. Also, I'm not sure what that would accomplish. – spitzanator Sep 16 '13 at 11:19

1 Answers1

0

Use android:layout_gravity="fill_horizontal" in the root of your RelativeLayout. That will cause the relative layout to actually expand to fill the action bar, in turn causing everything to layout properly.