Change Color TabSelector on v4 ViewPager

2019-04-05 19:55发布

问题:

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:

回答1:

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).



回答2:

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>


回答3:

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


回答4:

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!!