Change Color TabSelector on v4 ViewPager

梦想与她 提交于 2019-12-21 09:16:43

问题


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



来源:https://stackoverflow.com/questions/18077457/change-color-tabselector-on-v4-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!