Search code examples
androidandroid-actionbarandroid-custom-viewandroid-tabs

Android tabs custom view


I am adding tabs to my app with this code:

ActionBar.Tab tab1=actionBar.newTab();
tab1.setTabListener(this);
tab1.setCustomView(R.layout.tab_style);
TextView txt1 = (TextView)tab1.getCustomView().findViewById(R.id.tabtext);
txt1.setText(R.string.tab_1);
actionBar.addTab(tab1);

And this is the tab_style.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This is the main layout of the application -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_basic_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" 
    android:paddingBottom="15dp"
    >


<TextView
    android:id="@+id/tabtext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textSize="20dp"
/>
</RelativeLayout>

I want to change tab background,but result:

enter image description here

How can I fix it ? Customview is not centering.


Solution

  • action bar tabs are deprecated in the "L" Developer Preview.

    the black area is background of actionBar tabBar.you must style actionBarTabBarStyle and set the background of it. look at this link:

    http://blog.alwold.com/2013/08/28/styling-tabs-in-the-android-action-bar/

    or you can use other library like :

    PagerSlidingTabStrip

    or if you want to have a google one you can try:

    SlidingTabsBasic