更改选项卡的文本颜色

时间:2016-01-22 20:57:49

标签: java android xml

我正在尝试制作可滚动标签。我得到了所有工作,但我不知道如何更改单个选项卡的文本颜色,这是我的代码... XML(MainActivity):

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

    <android.support.v4.view.PagerTitleStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:layout_gravity="top"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:background="#000000"
        android:focusableInTouchMode="true"/>
</android.support.v4.view.ViewPager>

MainActivity.java:

public class MainActivity extends FragmentActivity {
    ViewPager viewPager = null;

    @Override
    protected void onCreate(Bundle savedInstanceStats) {
        super.onCreate(savedInstanceStats);
        setContentView(R.layout.homescreen);
        viewPager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));
    }
}

class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        if(position == 0) {
            fragment = new FragmentDominik();
        } else if(position == 1) {
            fragment = new FragmentTobias();
        } else if(position == 2) {
            fragment = new FragmentTom();
        } else if(position == 3) {
            fragment = new FragmentNikolas();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 4;
    }

    public CharSequence getPageTitle(int position) {
        String title = new String();
        if(position == 0) {
            return "Dominik";
        } else if(position == 1) {
            return "Tobias";
        } else if(position == 2) {
            return "Tom";
        } else if(position == 3) {
            return "Nikolas";
        }
        return null;
    }
}

所以默认的文字颜色是黑色,但是我希望文字是白色的(我已经知道了)背景是黑色的,这是一张图片:

i want the pink bar to be black and the text in the pink bar to be white

我是xml和java的新手,我只是使用在线教程制作这个可滚动的标签,所以任何帮助都很感激:)谢谢

1 个答案:

答案 0 :(得分:0)

我建议为你的PagerTitleStrip添加一种风格:

<android.support.v4.view.PagerTitleStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:layout_gravity="top"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:background="#000000"
        android:focusableInTouchMode="true"
        style="@style/your_style"
        />

然后在你的styles.xml中:

<style name="your_style">
    <item name="android:textColor">#00FF00</item>
</style>