android sliding tab,AndroidUI组件SlidingTabLayout实现ViewPager页滑动效果

本文介绍了如何使用自定义的SlidingTabLayout来实现ViewPager的滑动页指示器效果。通过修改SlidingTabLayout.java和SlidingTabStrip.java,可以定制选项卡的颜色、字体大小等样式。提供了SlidingTabLayout的完整代码,并在活动中展示了如何设置颜色、分割线和字体大小,以及如何与ViewPager配合使用。
摘要由CSDN通过智能技术生成

使用SlidingTabLayout需要准备2个类,分别是 SlidingTabLayout,与SlidingTabStrip,,放进项目中时只用修改下包名即可。

918af1c0eea8bc36cca90297835f4e92.gif

效果制作的不是很好。

这篇文章,也是在网上搜了很多资源参考,对 SlidingTabLayout.java和SlidingTabStrip.java进行了修改。大家可以更改他的格式字体大小、选中状态,分割线调整等等。先上传这两个文件,改动支出都做了注释。

SlidingTabLayout.java

/*

* Copyright (C) 2013 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.example.my.slidingtablayout;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Color;

import android.graphics.Typeface;

import android.os.Build;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.util.AttributeSet;

import android.util.TypedValue;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.HorizontalScrollView;

import android.widget.LinearLayout;

import android.widget.TextView;

/**

* To be used with ViewPager to provide a tab indicator component which give constant feedback as to

* the user's scroll progress.

*

* To use the component, simply add it to your view hierarchy. Then in your

* {@link android.app.Activity} or {@link android.support.v4.app.Fragment} call

* {@link #setViewPager(ViewPager)} providing it the ViewPager this layout is being used for.

*

* The colors can be customized in two ways. The first and simplest is to provide an array of colors

* via {@link #setSelectedIndicatorColors(int...)} and {@link #setDividerColors(int...)}. The

* alternative is via the {@link TabColorizer} interface which provides you complete control over

* which color is used for any individual position.

*

* The views used as tabs can be customized by calling {@link #setCustomTabView(int, int)},

* providing the layout ID of your custom layout.

*/

public class SlidingTabLayout extends HorizontalScrollView {

/**

* Allows complete control over the colors drawn in the tab layout. Set with

* {@link #setCustomTabColorizer(TabColorizer)}.

*/

public interface TabColorizer {

/**

* @return return the color of the indicator used when {@code position} is selected.

*/

int getIndicatorColor(int position);

/**

* @return return the color of the divider drawn to the right of {@code position}.

*/

int getDividerColor(int position);

}

private static final int TITLE_OFFSET_DIPS = 24;

private static final int TAB_VIEW_PADDING_DIPS = 16; //内边距

private static int TAB_VIEW_TEXT_SIZE_SP = 16; //字体大小

private int mTitleOffset;

private int mTabViewLayoutId;

private int mTabViewTextViewId;

// 定义两种需要添加的选项卡颜色

private int mDefaultTextColor;

private int mSelectedTextColor;

private ViewPager mViewPager;

private ViewPager.OnPageChangeListener mViewPagerPageChangeListener;

private final SlidingTabStrip mTabStrip;

public SlidingTabLayout(Context context) {

this(context, null);

}

public SlidingTabLayout(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

// 获取选项卡颜色,如果未定义的话,则使用主题默认的颜色

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.SlidingTabLayout);

int defaultTextColor = a.getColor(

R.styleable.SlidingTabLayout_android_textColorPrimary, 0);

mDefaultTextColor = a.getColor(

R.styleable.SlidingTabLayout_textColorTabDefault, defaultTextColor);

mSelectedTextColor = a.getColor(

R.styleable.SlidingTabLayout_textColorTabSelected ,defaultTextColor);

a.recycle();

// Disable the Scroll Bar

setHorizontalScrollBarEnabled(false);

// Make sure that the Tab Strips fills this View

setFillViewport(true);

mTitleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density);

mTabStrip = new SlidingTabStrip(context);

addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CON

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值