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. 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)【转】

    介绍 为其他对象提供一个代理以控制对这个对象的访问. 示例 有一个Message实体类,某对象对它的操作有Insert()和Get()方法,用一个代理来控制对这个对象的访问. MessageModel ...

  2. android开发之路08(ListView&Adapter)

    ListView控件介绍:用于将数据库中的数据或者网络中的数据通过列表的形式显示出来:ListView采用MVC模式将前端显示和后端数据进行分离. 也就是说,ListView控件在装载数据时并不是直接 ...

  3. generated key...

    1. javax.persistence.GeneratedValue @Data @MappedSuperclass public abstract class UUIDEntity { @Id @ ...

  4. [Android]自定义dialog

    吃过饭,有没有太写代码的愿望,就开始想今天工作中遇到的问题了. 有一个很有意思的东西就是Dialog,这个是基础的组件: 我们会在一些地方需要自定义弹出窗,可是用popUpWindow太小题大作,用弹 ...

  5. Linux下配置Node环境变量及问题详解

    这是之前在Linux下配置Node环境变量时踩过的坑,今天又有小伙伴询问这个问题,因此记录下来,不仅是给新童鞋们一些参考,也方便日后查阅 在这之前,相信都已经安装好了,没安装的可以查看博主另一篇文章 ...

  6. [改善Java代码]正确使用String,StringBuffer,StringBuilder

    CharSequence接口有三个实现类与字符串有关:String,StringBuffer,StringBuffer.虽然它们都与字符串有关,但是其处理机制是不同的. String类是不可改变的量, ...

  7. 关于Eclipse(MyEclipse)中一次性批量导入多个项目Project.

    以前更换Eclipse(MyEclipse)的时候要想把原Eclipse中的项目导入到新的Eclipse中的做法是: 1.先把原Eclipse中工作空间中的项目(不包括.metadata文件夹)复制到 ...

  8. #if...#endif的用法总结

    在写到  pwm音阶程序的时候,在代码中有#if...#endif的用法问题,相关音阶的代码如下: /************************************************* ...

  9. RestEasy传值方式

    一.@pathparam    @PathParam 是一个参数注解,可以将一个 URL 上的参数映射到方法的参数上,它可以映射到方法参数的类型有基本类型.字符串.或者任何有一个字符串作为构造方法参数 ...

  10. Java Concurrency - Concurrent Collections

    Data structures are a basic element in programming. Almost every program uses one or more types of d ...