版权声明:本文为博主原创文章,转载请注明出处。

控件简介

UISegmentControl在IOS平台的App中非常常见,其控件如下图所示:

这种控件的主要作用是动态的更改界面的显示内容,一般应用于内容较多的界面,且分屏显示不同种类的内容。

Android开发过程中,有时需要实现类似UISegmentControl的效果,这里我将自己的代码开源在github上了,命名为SegmentControlView,下面是实现的效果:

项目地址可点击如下链接: 
https://github.com/Carbs0126/AndroidSegmentControlView

控件说明

 SegmentControlView extends View

这个SegmentControlView是继承自View的,而非组合控件,因此实现起来虽然有点繁琐,但是灵活性反而比组合控件要高很多,并且可以加上过度效果等,且比组合控件更加轻量化。

控件特点

我实现的这个SegmentControlView具有如下特点:

  1.SegmentControlView可设置与ViewPager联动,在segment切换时具有颜色渐变效果,类似微信。
  2.可以自定义SegmentControlView的四个corner的半径。
  3.自定义背景颜色与字体颜色、字体大小等等。
  4.在xml中设置SegmentControlView中的多个titles,自动按照titles生成多个segment。
  5.设置按下颜色与normal颜色的色值对比度。
  6.具有AT_MOST的适配功能,即具有wrap_content模式
  7.可以设置回调响应事件

使用方法

属性文件说明

<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="SegmentControlView">
<attr name="scv_BackgroundSelectedColor" format="reference|color" />//选中segment的背景颜色
<attr name="scv_BackgroundNormalColor" format="reference|color" />//未选中segment的背景颜色
<attr name="scv_TextSelectedColor" format="reference|color" />//选中segment的文字颜色
<attr name="scv_TextNormalColor" format="reference|color" />//未选中segment的文字颜色
<attr name="scv_FrameColor" format="reference|color" />//segment边框的颜色
<attr name="scv_FrameWidth" format="reference|dimension" />//segment边框的宽度
<attr name="scv_FrameCornerRadius" format="reference|dimension" />//segment四个圆角的半径大小
<attr name="scv_TextSize" format="reference|dimension" />//文字大小
<attr name="scv_TextArray" format="reference" />//string数组,每一个string都会填充到一个segment中
<attr name="scv_SelectedIndex" format="reference|integer" />//默认选中的segment
<attr name="scv_SegmentPaddingHorizontal" format="reference|dimension" />//每一个segment内部的水平padding
<attr name="scv_SegmentPaddingVertical" format="reference|dimension" />每一个Segment的竖直方向的padding
<attr name="scv_Gradient" format="reference|boolean" />//Segment改变时是否使用颜色渐变效果
</declare-styleable> </resources>

布局文件创建SegmentControlView

<cn.carbs.android.segmentcontrolview.library.SegmentControlView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:scv_FrameCornerRadius="6dp"
app:scv_FrameWidth="1dp"
app:scv_Gradient="true"
app:scv_SegmentPaddingVertical="5dp"
app:scv_TextArray="@array/segment_control_arrays_0"/>

使用方法

dependencies {
compile 'cn.carbs.android:SegmentControlView:1.0.0'
}
 segmentcontrolview.setOnSegmentChangedListener(new SegmentControlView.OnSegmentChangedListener() {
@Override
public void onSegmentChanged(int newSelectedIndex) {
if(viewpager != null){
//change the second argument to true if you want the gradient effect when viewpager is changing
viewpager.setCurrentItem(newSelectedIndex, false);//viewpager changing without animation
}
}
});
//set viewpager to change segment according to the state of viewpager
segmentcontrolview.setViewPager(viewpager);
//set the selected index of segments initiatively
segmentcontrolview.setSelectedIndex();
//set gradient effect if you want
segmentcontrolview.setGradient(true);

项目地址:

https://github.com/Carbs0126/AndroidSegmentControlView

Git:

git clone https://github.com/Carbs0126/AndroidSegmentControlView.git

[原创]自定义控件之AndroidSegmentControlView,仿IOS平台UISegmentControlView,继承自View的更多相关文章

  1. iOS平台快速发布HT for Web拓扑图应用

    iOS平台一直是封闭的生态圈,iOS开发者要缴纳年费加入开发者计划才可进行iOS平台的APP开发测试,所开发的APP需要上传到App Store经过苹果审核以后才可对外发布.如果要开发企业内部应用,则 ...

  2. 原创开源项目HierarchyViewer for iOS 2.1 Beta新功能介绍

    回顾 HierarchyViewer for iOS是我们发布的一个开源项目,采用GPL v3.0协议. HierarchyViewer for iOS可以帮助iOS应用的开发和测试人员,在没有源代码 ...

  3. 教你pomeloclient包libpomelo增加cocos2d-x 3.0工程(Windows、Android、IOS平台)

    Windows平台 操作系统:Windows7(64-bit) VS版本号:2013 Cocos2d-x版本号:3.0 project路径:E:\cocos2d-prj\ 1.从github下载lib ...

  4. iOS平台添加Google Admob -1/2(Unity3D开发之七)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=567 Unity调用iOS还是非 ...

  5. Android-PickerView【仿iOS的PickerView控件,并封装了时间选择和选项选择这两种选择器】使用

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本文主要演示Android-PickerView的选项选择器.时间选择器的简单运用.由于每一个版本略有不用,所以实际使用方式以git ...

  6. iOS平台加入Google Admob -1/2(Unity3D开发之七)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com.谢谢! 原文地址: http://www.cocos2dev.com/?p=567 Unity调用iOS还是非 ...

  7. (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框

    Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框 标签: Android清除功能EditText仿IOS的输入框 2013-09-04 17:33 70865人阅读  ...

  8. (译)cocos2d-x跨android&ios平台开发入门教程

    免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...

  9. [转]ios平台内存常见问题

    本文转自CocoaChina,说的满详细的: 链接地址:http://www.cocoachina.com/bbs/read.php?tid=94017&keyword=%C4%DA%B4%E ...

随机推荐

  1. hdu 5101 n集合选2个不同集合数使和大于k

    http://acm.hdu.edu.cn/showproblem.php?pid=5101 给n个集合,选择两个来自不同集合的数,加和大于k,问有多少种选择方案. 答案=从所有数中选择的两个加和大于 ...

  2. 【原创】-- uboot,kennel,fs,rootfs 编译制作

    环境:ubuntu14.04  内核版本 linux 3.13.0   OK6410 内核编译环境   linux 3.0.1 uboot版本    1.1.6   交叉编译工具链   arm-lin ...

  3. python文件选择:tkFileDialog 基础

    tkFileDialog有两种形式: 一个是.askopenfilename(option=value, ...) 这个是"打开"对话框 另一个是:asksaveasfilenam ...

  4. python chardet简单应用

    python的字符串编码识别模块(第三方库): 官方地址: http://pypi.python.org/pypi/chardet   import chardet import urllib   # ...

  5. Entity Framework 4、5 多字段排序

    public interface IOrderByExpression<TEntity> where TEntity : class { IOrderedQueryable<TEnt ...

  6. Bad version number in .class file

    TY项目是用JDK1.6做的,早先在电脑上装了一个1.5的,这回就不能用了.为了能用,我就又装了一个1.6的,修改了环境变量之后,以为一切OK.开始测试,首先在Myeclipse中打开我用1.5编的一 ...

  7. [Hyper-V]使用操作系统模板创建新的虚拟机

    描述: 为了节省空间和时间的目的,先在Hyper-V里创建一个干净的操作系统,以后再创建虚拟机时都基于此操作系统,节省了安装Windows的时间 另外创建其它虚拟机的时候,也以上述虚拟机的磁盘为基础盘 ...

  8. p4 是否能自动merge

      总结: 1)如果在copy merge(-at)/auto merge(-am)后修改source branch,则可以自动被copy merge: 2)如果在manual merge后修改sou ...

  9. 玩转cocos2d-x lua-binding, 实现c++与lua混合编程

    引言 城市精灵GO(http://csjl.teamtop3.com/)是一款基于cocos2d-x开发的LBS社交游戏, 通过真实地图的探索, 发现和抓捕隐匿于身边的野生精灵, 利用游戏中丰富的玩法 ...

  10. CentOS挂载NTFS移动硬盘

    CentOS操作系统默认无法挂在NTFS格式的移动硬盘,解决方案之一为使用ntfs-3g挂在: 1. 在其官网上下载安装包: http://www.tuxera.com/community/open- ...