本文转载与:http://blog.csdn.net/zhangphil/article/details/48863347

Android SlidingTabLayout默认的滑动指示条是系统默认的某个蓝色系色值,分割线是灰色。如果要自定义实现滑动指示条和分割线定制颜色,则主要通过SlidingTabLayout的setCustomTabColorizer()方法实现。
现在给出一个例子加以说明。
(1)首先做一个MainActivity,此MainActivity没有实质意义,只是作为第二步加载要实现SlidingTabLayout Fragment的“容器”。
(2)在一个Fragment实现SlidingTabLayout,然后将此Fragment加载。

不要忘记引用Android官方实现的SlidingTabLayout和SlidingTabStrip。

代码层次结构如图所示:

测试用的主Activity MainActivity.java :

 package com.zzw.testsetcustomtabcolorizer;

 import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction; public class MainActivity extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
TabFragment fragment = new TabFragment();
transaction.replace(R.id.content_fragment, fragment);
transaction.commit();
}
}
}

MainActivity.java需要的布局文件:activity_main.xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zzw.testsetcustomtabcolorizer.MainActivity" > <FrameLayout
android:id="@+id/content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </RelativeLayout>

TabFragment.java代码文件:

 package com.zzw.testsetcustomtabcolorizer;

 import java.util.ArrayList;

 import com.zzw.testsetcustomtabcolorizer.SlidingTabLayout.TabColorizer;

 import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.TextView; public class TabFragment extends Fragment { private static class PagerItem {
private final CharSequence mTitle;
private final int mIndicatorColor;
private final int mDividerColor; public PagerItem(CharSequence mTitle, int mIndicatorColor,
int mDividerColor) {
super();
this.mTitle = mTitle;
this.mIndicatorColor = mIndicatorColor;
this.mDividerColor = mDividerColor;
} public Fragment createFragment() {
return ContentFragment.newInstance(mTitle, mIndicatorColor,
mDividerColor); } public CharSequence getTitle() {
return mTitle;
} public int getIndicatorColor() {
return mIndicatorColor;
} public int getDividerColor() {
return mDividerColor;
}
} private ArrayList<PagerItem> mTabCards = new ArrayList<PagerItem>(); public static class ContentFragment extends Fragment {
private static final String KEY_TITLE = "title";
private static final String KEY_INDICATOR_COLOR = "indicator_color";
private static final String KEY_DIVIDER_COLOR = "divider_color"; public static ContentFragment newInstance(CharSequence title,
int indicatorColor, int dividerColor) {
Bundle bundle = new Bundle();
bundle.putCharSequence(KEY_TITLE, title);
bundle.putInt(KEY_INDICATOR_COLOR, indicatorColor);
bundle.putInt(KEY_DIVIDER_COLOR, dividerColor); ContentFragment fragment = new ContentFragment();
fragment.setArguments(bundle); return fragment;
} @Override
@Nullable
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) { TextView tv = new TextView(getActivity());
tv.setGravity(Gravity.CENTER); return tv;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
TextView tv = (TextView) view; Bundle args = getArguments(); String content = "";
if (args != null) {
String title = args.getCharSequence(KEY_TITLE) + ""; int indicatorColor = args.getInt(KEY_INDICATOR_COLOR);
String indicatorColors = Integer.toHexString(indicatorColor)
+ ""; int dividerColor = args.getInt(KEY_DIVIDER_COLOR);
String dividerColors = Integer.toHexString(dividerColor) + ""; content = content + "标题:" + title + "\n";
content = content + "indicatorColor:" + indicatorColors + "\n";
content = content + "dividerColor:" + dividerColors;
}
tv.setText(content);
} } @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); mTabCards.add(new PagerItem("Tab A", Color.RED, Color.RED));
mTabCards.add(new PagerItem("Tab B", Color.YELLOW, Color.YELLOW));
mTabCards.add(new PagerItem("Tab C", Color.GREEN, Color.GREEN));
mTabCards.add(new PagerItem("Tab D", Color.BLUE, Color.BLUE));
mTabCards.add(new PagerItem("Tab E", Color.CYAN, Color.CYAN));
mTabCards.add(new PagerItem("Tab F", Color.MAGENTA, Color.MAGENTA));
} @Override
@Nullable
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, null);
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new MyFragmentPagerAdapter(
getChildFragmentManager())); SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) view
.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
      //设置颜色的代码
mSlidingTabLayout.setCustomTabColorizer(new TabColorizer() { @Override
public int getIndicatorColor(int position) {
return mTabCards.get(position).getIndicatorColor();
} @Override
public int getDividerColor(int position) { return mTabCards.get(position).getDividerColor();
}
}); } public class MyFragmentPagerAdapter extends FragmentPagerAdapter { public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
} @Override
public int getCount() {
return mTabCards.size();
} @Override
public CharSequence getPageTitle(int position) {
return mTabCards.get(position).getTitle();
} @Override
public Fragment getItem(int position) {
return mTabCards.get(position).createFragment();
} }
}

TabFragment.java需要的布局文件fragment.xml文件代码

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <com.zzw.testsetcustomtabcolorizer.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

(转)Android SlidingTabLayout定制分割线和指示条颜色的更多相关文章

  1. Android自定义进度条颜色

    这个没法了只能看源码了,还好下载了源码, sources\base\core\res\res\ 下应有尽有,修改进度条颜色只能找progress ,因为是改变样式,首先找styles.xml ? 1 ...

  2. Android ViewPager再探:增加滑动指示条

    上一篇:<Android ViewPager初探:让页面滑动起来> ViewPager只是左右滑动有些丑,也不知道当前位于第几页面. 可以在上方加入滑动指示条,来确定当前位置. 只需要修改 ...

  3. android:改动PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色

    1.改动PagerTabStrip中的背景颜色 我们在布局中直接设置background属性就可以: <android.support.v4.view.ViewPager android:id= ...

  4. android:更改PagerTabStrip背景颜色,标题字体样式、颜色和图标,以及指示条的颜色

    1.更改PagerTabStrip背景颜色 我们直接在布局中设置background属性可以: <android.support.v4.view.ViewPager android:id=&qu ...

  5. android:修改PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色

    1.修改PagerTabStrip中的背景颜色 我们在布局中直接设置background属性即可: <android.support.v4.view.ViewPager android:id=& ...

  6. ViewPager 详解(四)----自主实现滑动指示条

    前言:前面我们用了三篇的时间讲述了有关ViewPager的基础知识,到这篇就要进入点实际的了.在第三篇<ViewPager 详解(三)---PagerTabStrip与PagerTitleStr ...

  7. android gridview画分割线

    dongyangzhang android gridview画分割线,如图: 1.先上图: 2.具体实现代码: public class LineGridView extends GridView { ...

  8. Gradle 实现 Android 多渠道定制化打包

    Gradle 实现 Android 多渠道定制化打包 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在项目中遇到需要实现 Apk 多渠道.定制化打包, Google .百度查找了一些资料, ...

  9. android 自定义进度条颜色

    android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对Android自定义进度条的学习过程!   这个没法了只能看源码了,还好下载了源码, sources\b ...

随机推荐

  1. java中整数类型(short int long)的存储方式

    在java中的整数类型有四种,分别是 byte  short int long 其中byte只有一个字节 0或1,在此不详细讲解. 其他的三种类型如下: 1.基本类型:short 二进制位数:16包装 ...

  2. [HDU 3033] I love sneakers! (动态规划分组背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3033 题意:给你K种品牌,每种品牌有不同种鞋,现在每种品牌至少挑一款鞋,问获得的最大价值,如果不能每种 ...

  3. Windows 2008下部署Exchange Server 2007

    对于很多政府及企业来说,微软的邮件服务器Exchange Server都是一个不错的通信和协作平台选择,尤其新版邮件服务器Exchange Server 2007 和OCS的组合,在微软UC平台上创下 ...

  4. Linux下程序对拍_C++

    此博客需要付费才阅读,因为该博客实用性十分强,且十分容易理解 若需购买请联系博主,联系方式戳这 http://www.cnblogs.com/hadilo/p/5932395.html 主要介绍如何在 ...

  5. LibreOffice源码编译以及生成VS项目

    最权威的社区链接:https://wiki.documentfoundation.org/Development/BuildingOnWindows 也许英文好的人直接看wiki上的说明就能很容易的编 ...

  6. 蓄水池采样算法(Reservoir Sampling)

    蓄水池采样算法 问题描述分析 采样问题经常会被遇到,比如: 从 100000 份调查报告中抽取 1000 份进行统计. 从一本很厚的电话簿中抽取 1000 人进行姓氏统计. 从 Google 搜索 & ...

  7. 慕课网-安卓工程师初养成-3-8 Java中的条件运算符

    来源:http://www.imooc.com/code/1306 条件运算符( ? : )也称为 “三元运算符”. 语法形式:布尔表达式 ? 表达式1 :表达式2 运算过程:如果布尔表达式的值为 t ...

  8. cocos2d-x Android版游戏之中国移动SDK嵌入

    . 拷贝API 将SDK\runtime\CMBilling20007.jar拷贝至游戏工程的runtime目录下(或其他目录) ,但切记不能放在libs目录下编译,否则编译报错(如:bad rang ...

  9. SSH Secure Shell Client的傻瓜式使用方法

    说明:本记录仅是使用此软件的一种简单的操作方式,如果想深入研究,请做如下三件事: 1)到其官网了解她的前世今生 2)下载她.安装她.操作她(这一步需要不断的尝试.不断的深入.不断的探索,当然最好理论结 ...

  10. ViewPager撤消左右滑动切换功能

    ViewPager取消左右滑动切换功能 最近做项目要求某种情况下ViewPager不能滑动,那么我们只需要重写这个方法就可以禁止ViewPager滑动 IndexViewPager.java: imp ...