0.引言

(1)Android从4.0开始提供了switch的滑动开关效果组件,但是之前版本却没有

(2)很多时候我们写程序,都希望把有用的通用的通用的东西封装起来,以便以后重用。

本文根据组件开发思想,首先介绍android自定义控件,然后将自定义的控件封装为jar包。最为实现了一个滑动开关的例子。最后效果如图所示:

下面是开发步骤:

1.android自定义控件

自定义控件过程:建立一个应用程序,新建一个类,该类继承View类,并实现参数为(Context context,AttributeSet attrs)的构造函数,定义控件对应的xml布局文件,定义控件的属性文件attrs

2.封装为jar包

封装jar包有两种方法,一是在新建工程的时候就勾选Mark this project as a library

这样建立的就是库文件,但是这样的话建立项目的时候不利于调式,因此使用的二种方法;第二种方法是在建立调试好项目后将,选择项目右键——>属性——>在左边面板中选择Android,在面板中勾选Is Library

3.switchview实例

声明:本例子是在别人的基础上更改而来的部分代码版权属于他人

(1)建立工程switchview2

建立类SwitchView,SwitchView继承自LinearLayout

(2)新建布局文件switch_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sv_container"
android:layout_width="230dip"
android:layout_height="38dip"
android:background="@drawable/usage_list_dark" >

<ImageButton
android:id="@+id/iv_switch_cursor"
android:layout_width="120dip"
android:layout_height="36dip"
android:layout_centerVertical="true"
android:layout_marginLeft="0.5dip"
android:layout_marginRight="0.5dip"
android:background="@drawable/usage_list_green" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<TextView
android:id="@+id/switch_text_true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="开" />

<TextView
android:id="@+id/switch_text_false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="关" />
</LinearLayout>

</RelativeLayout>

布局文件包括外边框sv_container,滑动块iv_switch_cursor,以及显示文本的两个TextView

(3)在res->values文件夹下新建立控件的属性文件attrs.xml,里面建立了一个name为SwitchView的declare-styleable来自定义属性,属性包括当开关返回的是true时显示的文本,为false显示的文本,常见的是“是否”、“男女”等二选一的地方。container_Hight、container_Width、container_Background是表示的是背景高度、宽度、背景图或颜色,cursor_Hight、cursor_Width、cursor_Background表示的是滑动块的高度、宽度、背景图或颜色,这里背景一般都是用图片,因为颜色表示不出效果来,最终滑动开关的效果关键也要靠这些属性综合决定。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SwitchView">
<attr name="textTrue" format="string" />
<attr name="textFalse" format="string" />
<attr name="container_Hight" format="dimension" />
<attr name="container_Width" format="dimension" />
<attr name="cursor_Hight" format="dimension" />
<attr name="cursor_Width" format="dimension" />
<attr name="cursor_Background" format="reference|color" />
<attr name="container_Background" format="reference|color" />
</declare-styleable>
</resources>

(4)重构SwitchView方法。方法必须包含AttributeSet属性attrs,attrs通过context.obtainStyledAttributes(attrs, R.styleable.SwitchView);方法建立TypedArray与attrs.xml中的SwitchView对应,然后就可以为相应的控件赋值。

public SwitchView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initView();

//设置滑动开关的显示文本
TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.SwitchView);//TypedArray是一个数组容器
this.setTextTrue(styledAttrs.getString(R.styleable.SwitchView_textTrue));
this.setTextFalse(styledAttrs.getString(R.styleable.SwitchView_textFalse));

int c_h=(int) styledAttrs.getDimension(R.styleable.SwitchView_container_Hight, 38);
int c_w=(int)styledAttrs.getDimension(R.styleable.SwitchView_container_Width, 230);
int iv_h=(int) styledAttrs.getDimension(R.styleable.SwitchView_cursor_Hight,36);
int iv_w=(int)styledAttrs.getDimension(R.styleable.SwitchView_cursor_Width, 120);

//更改布局大小,用setLayoutParams报错
sv_container.getLayoutParams().height=c_h;
sv_container.getLayoutParams().width=c_w;
Drawable drawable1=styledAttrs.getDrawable(R.styleable.SwitchView_container_Background);
if(drawable1!=null)
sv_container.setBackground(drawable1);
sv_container.invalidate();
iv_switch_cursor.getLayoutParams().height=iv_h;
iv_switch_cursor.getLayoutParams().width=iv_w;
Drawable drawable2=styledAttrs.getDrawable(R.styleable.SwitchView_cursor_Background);
if(drawable1!=null)
iv_switch_cursor.setBackground(drawable2);
iv_switch_cursor.invalidate();

// iv_switch_cursor.setLayoutParams(new LayoutParams(iv_w,iv_h));
}

(5)按照第二步的方法封装为jar包

4.实例应用

新建一个工程项目SwitchViewExample,建立一个Activity类MainActivity.class

在项目的属性中选择Android,点击Add添加SwitcView的库

或着在项目SwitchView2项目的bin下面将switchview2.jar拷贝靠SwitchViewExample项目下的libs文件夹下面,通过添加外部jar包引用的方式加载进来。

在MainActivity对应的布局文件中添加前面自定义的控件,并设置对应的属性

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:sv="http://schemas.android.com/apk/res-auto/com.jiesai.ljp.switchview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/res/com.example.switchviewexample">

<com.jiesai.ljp.switchview.SwitchView
android:id="@+id/sv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:textTrue="男"
app:textFalse="女">
</com.jiesai.ljp.switchview.SwitchView>

</RelativeLayout>

在MainActivity类中可以实例化一个SwitchView,通过switchView.isChecked();可以判断滑动开关选择的是什么项,然后想做什么就可以随便了

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SwitchView switchView=(SwitchView)findViewById(R.id.sv_test);
boolean check=switchView.isChecked();
if(check){
Toast.makeText(this, "你选择了:男", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "你选择了:女", Toast.LENGTH_LONG).show();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

附源码下载地址:http://download.csdn.net/detail/luojianpingljp/5842129

android自定义控件——以滑动开关为例的更多相关文章

  1. Android自定义控件之自定义属性

    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...

  2. Android自定义控件之基本原理

    前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...

  3. android自定义控件(3)-自定义当前按钮属性

    那么还是针对我们之前写的自定义控件:开关按钮为例来说,在之前的基础上,我们来看看有哪些属性是可以自定义的:按钮的背景图片,按钮的滑块图片,和按钮的状态(是开还是关),实际上都应该是可以在xml文件中直 ...

  4. android自定义控件(1)-自定义控件属性

    那么还是针对我们之前写的自定义控件:开关按钮为例来说,在之前的基础上,我们来看看有哪些属性是可以自定义的:按钮的背景图片,按钮的滑块图片,和按钮的状态(是开还是关),实际上都应该是可以在xml文件中直 ...

  5. Android自定义控件之基本原理(一)

    前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...

  6. Android自定义控件之自定义属性(二)

    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...

  7. Android自定义控件之自定义ViewGroup实现标签云

    前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...

  8. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  9. Android自定义控件1

    概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...

随机推荐

  1. Wormholes 虫洞 BZOJ 1715 spfa判断负环

    John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地 ...

  2. mvc 请求处理管道

    原文 http://blog.csdn.net/wulex/article/details/41514795 当一个asp.net mvc应用程序提出请求,为了响应请求,包含一些请求执行流程步骤! 在 ...

  3. R语言排序函数汇总

    总结: 1.sort是直接对向量排序,返回原数值: 2.order先对数值排序,然后返回排序后各数值的索引: 3.rank返回原数据各项排名,有并列的情况: 4.arrange是dplyr包中的,可对 ...

  4. Largest Submatrix of All 1’s(思维+单调栈)

    Given a m-by-n (0,1)-matrix, of all its submatrices of all 1's which is the largest? By largest we m ...

  5. 队列 102 Binary Tree Level Order Traversal

    队列的基本应用 - 广度优先遍历 1)树 : 层序遍历: 2)图:无权图的最短路径. 使用队列来实现二叉树的层序遍历,需要多关注一个层数的信息 /** * Definition for a binar ...

  6. 简单易懂的VS-CODE C++环境配置(ACM向)

    网上教程比较繁琐,他们似乎要把vs-code变得无所不能,而我只是想代替dev进行简单的输入输出 所以大概花了1个多小时找到了能用的方法(中途还搞了个ubuntu子系统发现没啥用) 这里随便说下 1. ...

  7. 克隆kvm虚拟机报错ImportError: No module named 'requests.packages.urllib3'

    2018-06-21 更新系统造成kvm克隆命令报错 virt-clone -o centos--update-clone -n centos--maven-test -f /var/lib/vmdk ...

  8. H5常见问题 微信踩过得坑

    微信页面内 click事件 只在a链接的时候有效,如果是div或者span之类  一定要加上样式 cursor:pointer 点击事件才生效. <div style="cursor: ...

  9. python 爬虫系列08-同步斗图一波

    一波大图来袭 import requests from lxml import etree from urllib import request import os import re def par ...

  10. IDEA 14.0 (默认模式) 快捷键

    IDEA 14.0 (默认模式) 快捷键 1.Alt+Shift+R:重命名变量名.类名.方法名(使用已经使用过的) 2.Ctrl+O :重写方法 3.Alt+Shift+P :实现接口 4.Alt+ ...