(转)Android SlidingTabLayout定制分割线和指示条颜色
本文转载与: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定制分割线和指示条颜色的更多相关文章
- Android自定义进度条颜色
这个没法了只能看源码了,还好下载了源码, sources\base\core\res\res\ 下应有尽有,修改进度条颜色只能找progress ,因为是改变样式,首先找styles.xml ? 1 ...
- Android ViewPager再探:增加滑动指示条
上一篇:<Android ViewPager初探:让页面滑动起来> ViewPager只是左右滑动有些丑,也不知道当前位于第几页面. 可以在上方加入滑动指示条,来确定当前位置. 只需要修改 ...
- android:改动PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色
1.改动PagerTabStrip中的背景颜色 我们在布局中直接设置background属性就可以: <android.support.v4.view.ViewPager android:id= ...
- android:更改PagerTabStrip背景颜色,标题字体样式、颜色和图标,以及指示条的颜色
1.更改PagerTabStrip背景颜色 我们直接在布局中设置background属性可以: <android.support.v4.view.ViewPager android:id=&qu ...
- android:修改PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色
1.修改PagerTabStrip中的背景颜色 我们在布局中直接设置background属性即可: <android.support.v4.view.ViewPager android:id=& ...
- ViewPager 详解(四)----自主实现滑动指示条
前言:前面我们用了三篇的时间讲述了有关ViewPager的基础知识,到这篇就要进入点实际的了.在第三篇<ViewPager 详解(三)---PagerTabStrip与PagerTitleStr ...
- android gridview画分割线
dongyangzhang android gridview画分割线,如图: 1.先上图: 2.具体实现代码: public class LineGridView extends GridView { ...
- Gradle 实现 Android 多渠道定制化打包
Gradle 实现 Android 多渠道定制化打包 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在项目中遇到需要实现 Apk 多渠道.定制化打包, Google .百度查找了一些资料, ...
- android 自定义进度条颜色
android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对Android自定义进度条的学习过程! 这个没法了只能看源码了,还好下载了源码, sources\b ...
随机推荐
- Mingyang.net:No identifier specified for entity
org.hibernate.AnnotationException: No identifier specified for entity: net.mingyang.modules.system.C ...
- Js 时间与字符串转示例
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ASP.net 探针
<%@ Page Language="JScript" ContentType="text/html" ResponseEncoding="gb ...
- X86平台下嵌入式linux触摸屏解决方案(usb触摸屏控制器+完美校准方案+触摸屏QTE开发环境搭建)
一直在用X86平台,真心不想用WINCE和XPE,一些大的硬件供应商都不提供linux平台下的技术支持,比如研华的3343PC104系列的板子... 开发的问题如下: 1 USB控制器目前只有台湾和竹 ...
- DPdao
hrbust1053 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1053 #in ...
- c# 控制服务启动停止
public string StartService(string serviceName, bool serviceFlag) { try { using (System.ServiceProces ...
- LICEcap GIF 屏幕录制工具
LICEcap 是一款屏幕录制工具,支持导出git动画图片格式,简单好用.大小只有几百KB 运行之后,可以随意调整大小,右下角有开始/停止按钮. 压缩包:http://files.cnb ...
- 关键字 base 的作用
①调用基类上已被其他方法重写的方法,小栗子a如下: public class Father { public virtual void Show() { Console.WriteLine(" ...
- PZISP自动下载软件运行时出现“应用程序无法启动,因为应用程序的并行配置不正确”
在win7下以管理员身份运行“PZISP自动下载软件”时出现“应用程序无法启动,因为应用程序的并行配置不正确”时,是因为系统里面没有一些visual c++库 想一想,反正以后也要用上VS2010的, ...
- PfSense基于BSD的软件防火墙的安装、配置与应用
PfSense基于BSD的软件防火墙的安装.配置与应用 PfSense是一个FreeBSD下的免费开源的防火墙和路由器软件,他为了在X86平台上面建立一个高集成性的防火墙项目,下面就为大家展示如何配置 ...