在ViewPager v4中更改背景选项卡

时间:2014-11-28 16:55:40

标签: java android android-activity android-viewpager android-tabs

我正在尝试更改标签viewpager的颜色,但我已经尝试了所有内容并且不会更改颜色。 这是我正在尝试自定义选项卡的活动:LocationsActivity.java:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.jad.pholoc.util.LocationsFragmentPagerAdapter;


public class LocationsActivity extends ActionBarActivity {

    private ViewPager mPager;
    public static FragmentManager fragmentManager;
    private LocationsFragmentPagerAdapter fragmentPagerAdapter;
    private ActionBar mActionbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locations);

        mActionbar = getSupportActionBar();

        mActionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mPager = (ViewPager) findViewById(R.id.pager);

        fragmentManager = getSupportFragmentManager();

        ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mActionbar.setSelectedNavigationItem(position);
            }
        };

        mPager.setOnPageChangeListener(pageChangeListener);


        fragmentPagerAdapter = new LocationsFragmentPagerAdapter(
                fragmentManager);

        mPager.setAdapter(fragmentPagerAdapter);

        mActionbar.setDisplayShowTitleEnabled(true);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

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

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                mPager.setCurrentItem(tab.getPosition());
            }

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

        Tab tab = mActionbar.newTab().setText("Lista")
                .setTabListener(tabListener);

        mActionbar.addTab(tab);

        tab = mActionbar.newTab().setText("Mapa").setTabListener(tabListener);

        mActionbar.addTab(tab);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.locations, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.acercade) {
            Intent i = new Intent(this, AcercaDeActivity.class);
            startActivity(i);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

activity_locations.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

styles.xml:

<resources>
   <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/ActionBarStyle</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
    </style>

    <style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="titleTextStyle">@style/TitleBarTextColor</item>
        <item name="background">@color/blue_pholoc</item>
        <item name="android:backgroundStacked">@color/blue_pholoc</item>
    </style>

    <style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
    </style>

     <style name="ActionBarTabStyle.Example" parent="@style/Widget.AppCompat.Light.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_example</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
</resources>

活动的当前外观是这个(图片):

https://dl.dropboxusercontent.com/u/30731371/LocationsActivity.png

正如我所说,我想改变标签的颜色,但这是不可能的,只是看起来很灰。

PD:我是西班牙语,我的英文写得不好(谷歌翻译)。

1 个答案:

答案 0 :(得分:1)

您是否考虑过使用此库:https://github.com/astuetz/PagerSlidingTabStrip

然后你可以在你的布局中设置这样的颜色:

android:background="@color/memrise_blue"

更简单,它也减少了你的代码:)