Android SlidingTabLayout custom split line and indicator line color, android split line

Source: Internet
Author: User

Android SlidingTabLayout custom split line and indicator line color, android split line

This article Reprinted with: http://blog.csdn.net/zhangphil/article/details/48863347

The default slide indicator bar of Android SlidingTabLayout is a default blue color value and the split line is gray. If you want to customize the color of the slide indicator bar and split line, you can use the setCustomTabColorizer () method of SlidingTabLayout.
An example is provided.
(1) first, create a MainActivity. This MainActivity has no substantive significance, but serves as the "Container" for loading SlidingTabLayout Fragment in step 2 ".
(2) Implement saggregingtablayout in a Fragment and load the Fragment.

 

Do not forget to reference SlidingTabLayout and SlidingTabStrip officially implemented by Android.

Code hierarchy:


Main Activity MainActivity. java used for testing:

 1 package com.zzw.testsetcustomtabcolorizer; 2  3 import android.os.Bundle; 4 import android.support.v4.app.FragmentActivity; 5 import android.support.v4.app.FragmentTransaction; 6  7 public class MainActivity extends FragmentActivity { 8  9     @Override10     protected void onCreate(Bundle savedInstanceState) {11         super.onCreate(savedInstanceState);12         setContentView(R.layout.activity_main);13 14         if (savedInstanceState == null) {15             FragmentTransaction transaction = getSupportFragmentManager()16                     .beginTransaction();17             TabFragment fragment = new TabFragment();18             transaction.replace(R.id.content_fragment, fragment);19             transaction.commit();20         }21     }22 }

 

 

Layout file required by MainActivity. java: activity_main.xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     tools:context="com.zzw.testsetcustomtabcolorizer.MainActivity" > 6  7     <FrameLayout 8         android:id="@+id/content_fragment" 9         android:layout_width="match_parent"10         android:layout_height="match_parent" />11 12 </RelativeLayout>

 

 

TabFragment. java code file:

1 package com. zzw. testsetcustomtabcolorizer; 2 3 import java. util. arrayList; 4 5 import com. zzw. testsetcustomtabcolorizer. slidingTabLayout. tabColorizer; 6 7 import android. graphics. color; 8 import android. OS. bundle; 9 import android. support. annotation. nullable; 10 import android. support. v4.app. fragment; 11 import android. support. v4.app. fragmentManager; 12 import android. support. v4.app. fragmentPager Adapter; 13 import android. support. v4.view. viewPager; 14 import android. view. gravity; 15 import android. view. layoutInflater; 16 import android. view. view; 17 import android. view. viewGroup; 18 import android. webkit. webView. findListener; 19 import android. widget. textView; 20 21 public class TabFragment extends Fragment {22 23 private static class PagerItem {24 private final CharSequence mTitle; 25 Private final int mIndicatorColor; 26 private final int mDividerColor; 27 28 public PagerItem (CharSequence mTitle, int mIndicatorColor, 29 int mDividerColor) {30 super (); 31 this. mTitle = mTitle; 32 this. mIndicatorColor = mIndicatorColor; 33 this. mDividerColor = mDividerColor; 34} 35 36 public Fragment createFragment () {37 return ContentFragment. newInstance (mTitle, mIndicatorColor, 38 mDividerC Olor); 39 40} 41 42 public CharSequence getTitle () {43 return mTitle; 44} 45 46 public int getIndicatorColor () {47 return mIndicatorColor; 48} 49 50 public int getDividerColor () {51 return mDividerColor; 52} 53} 54 55 private ArrayList <PagerItem> mTabCards = new ArrayList <PagerItem> (); 56 57 public static class ContentFragment extends Fragment {58 private static final String KEY_TITLE =" Title "; 59 private static final String character =" indicator_color "; 60 private static final String KEY_DIVIDER_COLOR =" divider_color "; 61 62 public static ContentFragment newInstance (CharSequence title, 63 int indicatorColor, int dividerColor) {64 Bundle bundle = new Bundle (); 65 bundle. putCharSequence (KEY_TITLE, title); 66 bundle. putInt (KEY_INDICATOR_COLOR, indicatorColor); 67 bundl E. putInt (KEY_DIVIDER_COLOR, dividerColor); 68 69 ContentFragment fragment = new ContentFragment (); 70 fragment. setArguments (bundle); 71 72 return fragment; 73} 74 75 @ Override 76 @ Nullable 77 public View onCreateView (LayoutInflater inflater, 78 @ Nullable ViewGroup container, 79 @ Nullable Bundle handle) {80 81 TextView TV = new TextView (getActivity (); 82 TV. setGravity (Gravity. CENTER); 83 84 return TV; 85} 86 87 @ Override 88 public void onViewCreated (View view, @ Nullable Bundle savedInstanceState) {89 TextView TV = (TextView) view; 90 91 Bundle args = getArguments (); 92 93 String content = ""; 94 if (args! = Null) {95 String title = args. getCharSequence (KEY_TITLE) + ""; 96 97 int indicatorColor = args. getInt (KEY_INDICATOR_COLOR); 98 String indicatorColors = Integer. toHexString (indicatorColor) 99 + ""; 100 101 int dividerColor = args. getInt (KEY_DIVIDER_COLOR); 102 String dividerColors = Integer. toHexString (dividerColor) + ""; 103 104 content = content + "title:" + title + "\ n"; 105 content = content + "indicatorColor: "+ indicatorColors +" \ n "; 106 content = content +" dividerColor: "+ dividerColors; 107} 108 TV. setText (content); 109} 110 111} 112 113 @ Override114 public void onCreate (@ Nullable Bundle savedInstanceState) {115 super. onCreate (savedInstanceState); 116 117 mTabCards. add (new PagerItem ("Tab A", Color. RED, Color. RED); 118 mTabCards. add (new PagerItem ("Tab B", Color. YELLOW, Color. YELLOW); 119 mTabCards. add (new PagerItem ("Tab C", Color. GREEN, Color. GREEN); 120 mTabCards. add (new PagerItem ("Tab D", Color. BLUE, Color. BLUE); 121 mTabCards. add (new PagerItem ("Tab E", Color. CYAN, Color. CYAN); 122 mTabCards. add (new PagerItem ("Tab F", Color. MAGENTA, Color. MAGENTA); 123} 124 125 @ Override126 @ Nullable127 public View onCreateView (LayoutInflater inflater, 128 @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {129 return inflater. inflate (R. layout. fragment, null); 130} 131 132 @ overridemo-public void onViewCreated (View view, @ Nullable Bundle savedInstanceState) {134 ViewPager mViewPager = (ViewPager) view. findViewById (R. id. viewpager); 135 mViewPager. setAdapter (new MyFragmentPagerAdapter (136 getChildFragmentManager (); 137 138 SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) view139. findViewById (R. id. sliding_tabs); 140 mSlidingTabLayout. setViewPager (mViewPager); 141 // set the color code 142 mSlidingTabLayout. setCustomTabColorizer (new TabColorizer () {143 144 @ Override145 public int getIndicatorColor (int position) {146 return mTabCards. get (position ). getIndicatorColor (); 147} 148 149 @ Override150 public int getDividerColor (int position) {151 152 return mTabCards. get (position ). getDividerColor (); 153} 154}); 155 156} 157 158 public class MyFragmentPagerAdapter extends FragmentPagerAdapter {159 160 public MyFragmentPagerAdapter (FragmentManager fm) {161 super (fm ); 162} 163 164 @ Override165 public int getCount () {166 return mTabCards. size (); 167} 168 169 @ Override170 public CharSequence getPageTitle (int position) {171 return mTabCards. get (position ). getTitle (); 172} 173 174 @ Override175 public Fragment getItem (int position) {176 return mTabCards. get (position ). createFragment (); 177} 178 179} 180}

 

 

Layout File fragment. xml Code required by TabFragment. java

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <com.zzw.testsetcustomtabcolorizer.SlidingTabLayout 8         android:id="@+id/sliding_tabs" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content" />11 12     <android.support.v4.view.ViewPager13         android:id="@+id/viewpager"14         android:layout_width="match_parent"15         android:layout_height="wrap_content" />16 </LinearLayout>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.