ActionBar自定义选项卡以编程方式

时间:2014-12-11 11:43:44

标签: android background tabs android-actionbar

我正在尝试以编程方式在ActionBar中创建自定义标签。我有2个错误:

1 - 我想根据选择或未选中选项卡来更改文本颜色,但我没有得到更改。始终tab1具有所选标签的颜色。

2 - 我的标签的背景不完整。 enter image description here

我需要帮助。谢谢

JAVA

private void cargarTabs() {

    TabsPagerAdapter mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    viewPager.setCurrentItem(0);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    cargarTextoTabs();

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

private void cargarTextoTabs() {

    nombresTabs = new String[2];

    switch (perfilObj.getIdioma()) {
    case 1:
        nombresTabs[0] = Espanol.MAIN_LISTADO;
        nombresTabs[1] = Espanol.MAIN_MAPA;

        break;

    case 2:
        nombresTabs[0] = Ingles.MAIN_LISTADO;
        nombresTabs[1] = Ingles.MAIN_MAPA;

        break;
    }

    for (int i = 0; i < nombresTabs.length; i++) {

        Tab tab = actionBar.newTab();

        tab.setTabListener(this);

        switch (i) {
        case 0:
            tab.setCustomView(cargarTabSel(i));
            break;

        default:
            tab.setCustomView(cargarTabUnSel(i));
            break;
        }

        actionBar.addTab(tab, i);
    }   
}

private View cargarTabSel(int i) {

    RelativeLayout r = (RelativeLayout) getLayoutInflater().inflate(
            R.layout.tab, null);
    TextView tv = (TextView) r.findViewById(R.id.tab_texto);
    RelativeLayout rl = (RelativeLayout) r.findViewById(R.id.tab_fondo);

    int fondo = 0, color = 0;

    switch (perfilObj.getColor()) {
    case 1:
        color = R.color.blanco;
        fondo = R.drawable.tab_indicator_negro;
        break;

    case 2:
        color = R.color.grisOscuro;
        fondo = R.drawable.tab_indicator_blanco;
        break;

    default:
        color = R.color.blanco;
        fondo = R.drawable.tab_indicator_negro;
    }

    tv.setText(nombresTabs[i]);
    tv.setTextColor(getResources().getColor(color));
    tv.setTextSize(14);
    tv.setTypeface(Modulo.fontUbuntuBold(this));
    tv.setEllipsize(TruncateAt.END);
    tv.setSingleLine();
    tv.setPadding(5, 0, 5, 0);

    rl.setBackgroundResource(fondo);

    return r;
}

private View cargarTabUnSel(int i) {

    RelativeLayout r = (RelativeLayout) getLayoutInflater().inflate(
            R.layout.tab, null);
    TextView tv = (TextView) r.findViewById(R.id.tab_texto);
    RelativeLayout rl = (RelativeLayout) r.findViewById(R.id.tab_fondo);

    int fondo = 0;

    switch (perfilObj.getColor()) {
    case 1:
        fondo = R.drawable.tab_indicator_negro;
        break;

    case 2:
        fondo = R.drawable.tab_indicator_blanco;
        break;

    default:
        fondo = R.drawable.tab_indicator_negro;
    }

    tv.setText(nombresTabs[i]);
    tv.setTextColor(getResources().getColor(R.color.gris));
    tv.setTextSize(14);
    tv.setTypeface(Modulo.fontUbuntu(this));
    tv.setEllipsize(TruncateAt.END);
    tv.setSingleLine();
    tv.setPadding(5, 0, 5, 0);

    rl.setBackgroundResource(fondo);

    return r;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {

    int pos = tab.getPosition();

    viewPager.setCurrentItem(pos);

    tab.setCustomView(cargarTabSel(pos));
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    tab.setCustomView(cargarTabUnSel(tab.getPosition()));
}

tab.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_fondo"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tab_texto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"/>

</RelativeLayout>

0 个答案:

没有答案