在v4 ViewPager上更改颜色TabSelector

时间:2013-08-06 10:21:24

标签: android android-viewpager android-custom-view android-tabs

是否可以更改v4 ViewPager上所选标签的颜色?
我需要使用v4 ViewPager,但我没有找到任何来源来定制它 只是为了澄清我需要将蓝色更改为另一种颜色:

TabSelector

4 个答案:

答案 0 :(得分:11)

这是标签指示器。您可以通过应用不同的样式来更改其颜色。

使用Action Bar Style Generator,生成9patch png文件(tab_selected,tab_selected_focused等),并将这些文件+样式添加到项目中。

另一种方法 - > How to change the current tab highlighter color in Android ViewPager?(正如@Pratik在评论中写的那样)。

答案 1 :(得分:3)

我没有足够的声誉来评论答案,但关于Action Bar Style Generator确保在将文件添加到项目中的相应文件夹之后,您还要将主题添加到清单xml文件中这样:

<activity
    android:name="com.whatever.myapplication.YourActivityName"
    android:theme="@style/Theme.Whatever_you_named_your_style_in_the_generator">
</activity>

答案 2 :(得分:1)

ViewPager不是您需要自定义的那个。您需要在布局中设置与tabIndicatorColor相关联的TabLayout

动态地你可以做

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout_id);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setSelectedTabIndicatorColor(R.color.your_color); // here

在XML中,这就像下面的

一样简单
<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="@color/your_color" />

答案 3 :(得分:0)

同样的方法我找不到自定义标签的方法。所以我用

修复了它
<View
        android:layout_height="2dp"
        android:id="@+id/line1"
         android:layout_width="fill_parent"
        android:layout_below="@+id/headertab1"
       android:layout_above="@+id/viewpager"
        android:background="#0066CC" />

我把这个代码放在每个3个标签上,然后是标签&amp;在viewPager之上。 因为我们可以很容易地检测到哪个选项卡被选中。 因此,我们可以将此“line1”可见性用于View.VISIBLE或View.INVISIBLE。

希望对你有所帮助!!