Java 更改 ViewPager 指示器颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20028342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:27:13  来源:igfitidea点击:

Change ViewPager Indicator color

javaandroideclipseandroid-viewpager

提问by clever_trevor

I am trying to change the color of my ViewPager Indicator. I've read on herethat it uses a nine-patch, but I do not know where to find it. Here is my relevant Java code for the ViewPager creation.

我正在尝试更改 ViewPager 指示器的颜色。我在这里读到它使用了九个补丁,但我不知道在哪里可以找到它。这是我创建 ViewPager 的相关 Java 代码。

/**
 * On swiping the ViewPager make respective tab selected
 **/
private void initializeViewPagerListener() {
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

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

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

/**
 * Creates the tab portion of the ViewPager
 */
private void initializeTabs() {
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

}

采纳答案by bigsolom

I've created a custom ViewPager Indicator (nexus-5 launcher style) with custom attribute available to change the circles color

我创建了一个自定义 ViewPager 指标(nexus-5 启动器样式),自定义属性可用于更改圆圈颜色

https://github.com/bigsolom/AndroidViewPagerIndicator

https://github.com/bigsolom/AndroidViewPagerIndicator

回答by mako

Which ViewPager are you using? From support library or from which Android version?

您使用的是哪个 ViewPager?来自支持库还是来自哪个 Android 版本?

If it is android.support.v4.view.ViewPager you should only add one line of code:

如果是 android.support.v4.view.ViewPager 你应该只添加一行代码:

    viewPager.getChildAt(0).setBackgroundResource(R.color.blue);

And also define blue color(or any other color chosen by you) in res/values/color.xml file:

并在 res/values/color.xml 文件中定义蓝色(或您选择的任何其他颜色):

    <color name="blue">#015599</color>

This should help.

这应该有帮助。

回答by nima72

from the ViewPagerIndicator document from the below link:

来自以下链接的 ViewPagerIndicator 文档:

AndroidViewPagerIndicator

AndroidViewPagerIndicator

you can set this: Add the custom view below your view pager, the available attributes are: radius that specifies the radius of the circles and color to specify the color of them

你可以这样设置:在你的视图寻呼机下面添加自定义视图,可用的属性是:radius 指定圆的半径和 color 来指定它们的颜色

<com.efoad.views.ViewPagerIndicator
android:id="@+id/viewpager_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:padding="5dp"
custom:radius="3dp"
custom:color="@android:color/darker_gray" />

回答by bambi

PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_title_strip);
pagerTabStrip.setTabIndicatorColor(Color.RED);

回答by Madi

Write this line on your java class indicator.setFillColor(Color.RED);

在你的java类上写下这一行 indicator.setFillColor(Color.RED);