http://blog.csdn.net/qduningning/article/details/37935227

res/drawable/seg_left.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true">
<shape >
<stroke android:color="#0079FF" android:width="1dp"/>
<solid android:color="#0079FF"/>
<corners android:topLeftRadius="3dp" android:bottomLeftRadius="3dp" android:topRightRadius="0dp" android:bottomRightRadius="0dp"/>
</shape>
</item>
<item>
<shape >
<stroke android:color="#0079FF" android:width="1dp"/>
<solid android:color="#FFFFFF"/>
<corners android:topLeftRadius="3dp" android:bottomLeftRadius="3dp" android:topRightRadius="0dp" android:bottomRightRadius="0dp"/>
</shape>
</item>
</selector>

res/drawable/seg_right.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true">
<shape >
<stroke android:color="#0079FF" android:width="1dp"/>
<solid android:color="#0079FF"/>
<corners android:topLeftRadius="0dp" android:bottomLeftRadius="0dp" android:topRightRadius="3dp" android:bottomRightRadius="3dp"/>
</shape>
</item>
<item>
<shape >
<stroke android:color="#0079FF" android:width="1dp"/>
<solid android:color="#FFFFFF"/>
<corners android:topLeftRadius="0dp" android:bottomLeftRadius="0dp" android:topRightRadius="3dp" android:bottomRightRadius="3dp"/>
</shape>
</item>
</selector>

字体颜色: res/drawable/seg_text_color_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:color="#FFFFFF"/>
<item android:color="#0079FF"/>
</selector>
    package cn.haiwan.app.widget;  

    import org.xmlpull.v1.XmlPullParser;  

    import android.R.integer;
import android.content.Context;
import android.content.res.ColorStateList;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.haiwan.R; public class SegmentView extends LinearLayout {
private TextView textView1;
private TextView textView2;
private onSegmentViewClickListener listener;
public SegmentView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public SegmentView(Context context) {
super(context);
init();
} private void init() {
// this.setLayoutParams(new LinearLayout.LayoutParams(dp2Px(getContext(), 60), LinearLayout.LayoutParams.WRAP_CONTENT));
textView1 = new TextView(getContext());
textView2 = new TextView(getContext());
textView1.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
textView2.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
textView1.setText("SEG1");
textView2.setText("SEG2");
XmlPullParser xrp = getResources().getXml(R.drawable.seg_text_color_selector);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
textView1.setTextColor(csl);
textView2.setTextColor(csl);
} catch (Exception e) {
}
textView1.setGravity(Gravity.CENTER);
textView2.setGravity(Gravity.CENTER);
textView1.setPadding(3, 6, 3, 6);
textView2.setPadding(3, 6, 3, 6);
setSegmentTextSize(16);
textView1.setBackgroundResource(R.drawable.seg_left);
textView2.setBackgroundResource(R.drawable.seg_right);
textView1.setSelected(true);
this.removeAllViews();
this.addView(textView1);
this.addView(textView2);
this.invalidate(); textView1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (textView1.isSelected()) {
return;
}
textView1.setSelected(true);
textView2.setSelected(false);
if (listener != null) {
listener.onSegmentViewClick(textView1, 0);
}
}
});
textView2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (textView2.isSelected()) {
return;
}
textView2.setSelected(true);
textView1.setSelected(false);
if (listener != null) {
listener.onSegmentViewClick(textView2, 1);
}
}
});
} public void setSegmentTextSize(int dp) {
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, dp);
textView2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, dp);
} private static int dp2Px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
} public void setOnSegmentViewClickListener(onSegmentViewClickListener listener) {
this.listener = listener;
} public void setSegmentText(CharSequence text,int position) {
if (position == 0) {
textView1.setText(text);
}
if (position == 1) {
textView2.setText(text);
}
} public static interface onSegmentViewClickListener{
public void onSegmentViewClick(View v,int position);
}
}
<?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" > <cn.haiwan.app.widget.SegmentView
android:layout_width="160dp"
android:layout_height="wrap_content"android:layout_centerHorizontal="true"
/>
</LinearLayout>

模仿 ios 分段单选的更多相关文章

  1. js模仿ios select效果

    github:https://github.com/zhoushengmufc/iosselect webapp模仿ios下拉菜单 html下拉菜单select在安卓和IOS下表现不一样,iossel ...

  2. Android模仿iOS iMessages10照片选择器的实现

    不知不觉已经接近半年多没有写过博客了,这段时间,也是我刚好毕业走出校园的时间,由于学习工作的原因,一直没有真正静下心来写下些什么东西.这个星期刚入了小米笔记本pro的坑,本着新电脑新生活的理念嘻嘻-- ...

  3. iOS分段选择器、旅行App、标度尺、对对碰小游戏、自定义相册等源码

    iOS精选源码 企业级开源项目,模仿艺龙旅行App 标签选择器--LeeTagView CSSegmentedControl常用的分段选择器,简单易用! 仿微信左滑删除 IOS左滑返回 输入框 iOS ...

  4. ul、li模仿ios的TableView实现城市选择

    最近项目一个接着一个,之前说的精创环的项目还没做完,今天说先把那个放一下,先做访客系统,销售会见客户之后可以对客户进行一个跟踪记录,原型图也给了,今日头条的频道自定义页面一样. 如果是在IOS上让我来 ...

  5. 最新模仿ios版微信应用源码

    这个是刚刚从那个IOS教程网http://ios.662p.com分享来的,也是一个很不错的应用源码,仿微信基本功能.基于XMPP服务器的即时通信以及交友客户端. ----第一期代码的功能如下---- ...

  6. iOS/swift 单选框和复选框

    /** 复选框 */ import UIKit class LYBmutipleSelectView: UIView { var selectindexs:[Int]=[]//选中的 //标题数组 v ...

  7. 模仿ios下的coverflow

    Android高级图片滚动控件,编写3D版的图片轮播器 http://blog.csdn.net/guolin_blog/article/details/17482089 A cool Open So ...

  8. iOS - UITableView 单选功能实现

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property(nonatomic,copy)NSStrin ...

  9. iOS:iOS开发非常全的三方库、插件等等

    iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...

随机推荐

  1. Linux下vim配置详解

    转自http://www.cnblogs.com/witcxc/archive/2011/12/28/2304704.html

  2. Python学习 之 爬虫

    目标:下载贴吧或空间中所有图片 步骤:(1)获取页面代码 (2)获取图片URL,下载图片 代码如下: #!/usr/bin/python import re import urllib def get ...

  3. 20种常用的DOS命令小结

    先介绍一下通配符的概念. 通配符*和? *表示一个字符串 ?只代表一个字符 注意通配符只能通配文件名或扩展名,不能全都表示.例如我们要查找以字母y开头的所有文件,可以输入以下命令:dir y*.*:如 ...

  4. VBA Excel 打印

    1. 设置 页边距.打印区域 With .PageSetup .HeaderMargin = Application.CentimetersToPoints(0.5) .LeftMargin = Ap ...

  5. 如何扩大VMware虚拟机的硬盘磁盘空间大小

    首先,在虚拟机配置界面通过界面配置,直接扩大虚拟机硬盘大小: 而后,登陆虚拟机,在windows磁盘管理,更多操作中直接“重新扫描磁盘”,操作系统自动找到了多出来的磁盘空间: 最后,在老磁盘分区上通过 ...

  6. argparse - 命令行选项与参数解析(转)

    argparse - 命令行选项与参数解析(译)Mar 30, 2013 原文:argparse – Command line option and argument parsing 译者:young ...

  7. javaweb学习总结五(内省、beanUtils工具包)

    一:内省的概念 1:内省是反射的一种特例,由于在反射中频繁的操作javabean,所以为了方便反射 javabean,sun公司开发出一套API提高效率. 2:javaBean,就是用来封装客户端请求 ...

  8. [改善Java代码]推荐覆写toString方法

    建议49: 推荐覆写toString方法 为什么要覆写toString方法,这个问题很简单,因为Java提供的默认toString方法不友好,打印出来看不懂,不覆写不行,看这样一段代码: public ...

  9. 4k 对齐,你准备好了吗?

    什么是 4k 对齐? 其实与 “ 4K对齐 ” 相关联的是一个叫做 “ 高级格式化 ” 的分区技术.“ 高级格式化 ” 是国际硬盘设备与材料协会为新型数据结构格式所采用的名称.这是主要鉴于目前的硬盘容 ...

  10. 1172 Hankson 的趣味题

    1172 Hankson 的趣味题 2009年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Descrip ...