Change Color TabSelector on v4 ViewPager

2019-04-05 19:19发布

Is it possible to change color of selected tab on v4 ViewPager?
I need to use v4 ViewPager, but I don't find any source to customize it.
Just to clarify I need to change the blue color to another one:

TabSelector

4条回答
Explosion°爆炸
2楼-- · 2019-04-05 19:59

The ViewPager is not the one you need to customize. You need to set the tabIndicatorColor of the TabLayout linked with it in the layout.

Dynamically you could do

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

Inside the XML, that would be as simple as the following

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="@color/your_color" />
查看更多
叼着烟拽天下
3楼-- · 2019-04-05 20:09

This is the tab indicator. You can change its color by applying different styles.

Use Action Bar Style Generator, generate 9patch png files (tab_selected, tab_selected_focused etc.) and add these files + styles to your project.

Another approach -> How to change the current tab highlighter color in Android ViewPager? (as @Pratik wrote in comment).

查看更多
放我归山
4楼-- · 2019-04-05 20:11

I don't have enough reputation to comment on an Answer, but regarding the Action Bar Style Generator make sure after you add the files to the corresponding folders in your project that you also add the theme to your manifest xml file like this:

<activity
    android:name="com.whatever.myapplication.YourActivityName"
    android:theme="@style/Theme.Whatever_you_named_your_style_in_the_generator">
</activity>
查看更多
家丑人穷心不美
5楼-- · 2019-04-05 20:16

Same way i don't find the way to customize the tab. So i have fixed it using

<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" />

I have put this code with each 3 tabs belove the tab & above viewPager. As we can detect that which tab is selected very easily. So we can use this 'line1' visibility to View.VISIBLE or View.INVISIBLE.

Hope it helps to you!!

查看更多
登录 后发表回答