android自定义控件——以滑动开关为例
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自定义控件——以滑动开关为例的更多相关文章
- Android自定义控件之自定义属性
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- Android自定义控件之基本原理
前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...
- android自定义控件(3)-自定义当前按钮属性
那么还是针对我们之前写的自定义控件:开关按钮为例来说,在之前的基础上,我们来看看有哪些属性是可以自定义的:按钮的背景图片,按钮的滑块图片,和按钮的状态(是开还是关),实际上都应该是可以在xml文件中直 ...
- android自定义控件(1)-自定义控件属性
那么还是针对我们之前写的自定义控件:开关按钮为例来说,在之前的基础上,我们来看看有哪些属性是可以自定义的:按钮的背景图片,按钮的滑块图片,和按钮的状态(是开还是关),实际上都应该是可以在xml文件中直 ...
- Android自定义控件之基本原理(一)
前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...
- Android自定义控件之自定义属性(二)
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- Android自定义控件之自定义ViewGroup实现标签云
前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- Android自定义控件1
概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...
随机推荐
- [WebShow系列] 固定展示界面的现场调用
正在制作......,敬请期待. 固定展示界面的现场调用 现场管理员通过现场控制台可以控制主展示界面,实现 主题 所选选手展示 所选选手打分展示 排行展示 详情排行展示 柱状图展示 界面的展示切换外, ...
- 斐讯 N1 刷 Armbian 5.75
前言 不知不觉居然鸽了快半年的博客_(:3」∠)_ 好吧最近发现之前玩的 N1 Armbian 系统已经出到 5.75 了,之前刷 5.64 玩过,具体博文在此,说实话并不是很稳定,有线网络有时会卡死 ...
- CentOS 7 系统区域和键盘设置
系统区域设置包括语言.键盘布局等,可以通过修改/etc/locale.conf配置文件或使用localectl实用程序来进行设置,一般在安装时设置为默认选择(英文.美式键盘)即可 修改配置文件 [ro ...
- FTP 两种连接模式
简介 FTP协议要用到两个TCP连接, 一个是命令连接,用来在FTP客户端与服务器之间传递命令:另一个是数据连接,用来上传或下载数据.通常21端口是命令端口,20端口是数据端口.当混入主动/被动模式的 ...
- sharepoint_study_5
描述:手动进行SharePoint网页调试图解 解决: 第一步:打开页面的后台代码,设置断点 第二步:添加到进程 第三步:选择SharePoint进程,我这里都选了,如果你知道要调试的页面是哪一个进程 ...
- sharepoint_study_1
描述:机器上进行SharePoint开发,需要SQL Server提供最基本的服务 解决: SQL Server 的数据库引擎: SQL Server 代理: SQL Server 浏览器组件:
- 【KMP】【字符串】KMP字符串匹配算法 学习笔记
一.简介 KMP是由Knuth.Morris和Prat发明的字符串匹配算法,它的时间复杂度是均摊\(O(n+m)\).其实用Hash也可以做到线性,只不过Hash存在极其微小的难以避免的冲突. ...
- MBR为什么不支持3T硬盘
MBR,全称为Master Boot Record,即硬盘的主引导记录.(是管理硬盘分区的一种模式.升级版是GPT) MBR保存在硬盘的第1个扇区(即硬盘的0柱面.0磁头.1扇区).它由三个部分组成, ...
- spark java wordCount实例
1. 算子 package com.test; import java.util.Arrays; import java.util.List; import org.apache.spark.Spar ...
- JavaScript的type属性等于text/html 例子
在使用JavaScript标签<script>的时候,其中type最常用的就是text/javascript 其实这个type还有其他用法,下面直接给出例子: type属性为text/ht ...