Android自定义控件及自定义属性
Android自定义控件及自定义属性
自定义控件
创建自定义控件
自定义一个类,继承View
继承View还是哪个类,取决于你要实现一个什么样的控件
如果你要实现的是一个线性布局的组合控件,就可以继承LinearLayout
如果你要实现的是一个布局复杂的组合控件,就可以继承RelativeLayout
具体根据实际情况
这里我要实现一个Android端的显示验证码的控件,我只继承View
package ……;
import ……
/**
* Created by kongqw on 2015/10/23.
*/
public class CheckView extends View {
……
public CheckView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
……
}
public void onDraw(Canvas canvas) {
// 画界面
……
}
……
}
类似的,如果你是继承了RelativeLayout,大概可以这样实现
package ……;
import ……
/**
* Created by kongqw on 2015/7/10.
*/
public class KTop extends RelativeLayout {
private ……
……
public KTop(Context context) {
super(context);
initView();
}
public KTop(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public KTop(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
* 初始化界面
*/
private void initView() {
view = View.inflate(getContext(), R.layout.k_top, this);
// 控件背景
mTitleView = (RelativeLayout) view.findViewById(R.id.title_view);
// 只举一个例子,这里可以获取的布局里的控件
……
}
// 做一些其他操作的处理,例如控件的点击事件处理等
……
}
使用自定义控件
在布局文件中的使用
<kong.qingwei.demo.kqwcheckviewdemo.CheckView
android:id="@+id/checkView"
android:layout_width="wrap_content"
android:layout_height="50dp" />
自定义属性
定义自定义属性
在values文件夹下创建attrs.xml文件
name是自定义属性的名称
format是自定义属性的类型,有如下类型,就不一一介绍了
代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CheckView">
<!-- 随机点数 -->
<attr name="point_num" format="integer" />
<!-- 随机线数 -->
<attr name="point_line" format="integer" />
<!-- 验证码长度 -->
<attr name="text_length" format="integer" />
<!-- 验证码字体大小-->
<attr name="text_size" format="integer" />
<!-- 验证码字体颜色 -->
<attr name="text_color" format="color" />
</declare-styleable>
</resources>
使用自定义属性
在使用自定义控件的xml文件里引入命名空间
xmlns:kongqw="http://schemas.android.com/apk/res-auto"
自定义属性的使用
kongqw:point_num="5"
示例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:kongqw="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
……
<kong.qingwei.demo.kqwcheckviewdemo.CheckView
android:id="@+id/checkView"
android:layout_width="wrap_content"
android:layout_height="50dp"
kongqw:point_num="5" />
……
</LinearLayout>
效果图
Android自定义控件及自定义属性的更多相关文章
- Android自定义控件之自定义属性
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- Android自定义控件之自定义属性(二)
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- android 自定义控件,自定义属性设置
做listView的上拉下拉刷新,网上找了个历程.但是有些界面只有上拉刷新,有些界面是下拉刷新.觉得应该在xml里定义一个属性控制上下拉使能. 0.关于自定义控件: 自定义控件设计主要方式有:a) 继 ...
- android自定义控件(三) 自定义属性
书接上回 在xml里建立属性,然后java代码里用typedArray获得这些属性,得到属性后,利用属性做一些事.例:得到xml里的color,赋给paint. 1.在res/values/下新建at ...
- Android自定义控件之自定义ViewGroup实现标签云
前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- Android自定义控件之基本原理
前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...
- Android自定义控件之自定义组合控件(三)
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- Android开发技巧——自定义控件之自定义属性
Android开发技巧--自定义控件之自定义属性 掌握自定义控件是很重要的,因为通过自定义控件,能够:解决UI问题,优化布局性能,简化布局代码. 上一篇讲了如何通过xml把几个控件组织起来,并继承某个 ...
随机推荐
- [LeetCode] Cracking the Safe 破解密码
There is a box protected by a password. The password is n digits, where each letter can be one of th ...
- [LeetCode] IP to CIDR 将IP地址转为CIDR无类别域间路由
Given a start IP address ip and a number of ips we need to cover n, return a representation of the r ...
- Cmder Windows 下的终端神器
废话 Windows 下常用的终端有两个,古老的 cmd 和功能强大但你记不住函数的 PowerShell ,两者我都用过一段时间,给我的提体验是功能够用,界面丑陋,虽然 win10 下可以通过调整背 ...
- [Ubuntu 16.04 LTS ]屏幕分辨率 Unknown display
新装完Ubuntu 16.04 LTS 通过System settings-->Displays 设置屏幕分辨率 显示"Unknown display",选择后无反应,并且屏 ...
- PHPCMS v9.5.8-设计缺陷可重置前台任意用户密码
验证.参考漏洞:http://wooyun.jozxing.cc/static/bugs/wooyun-2015-0152291.html 漏洞出现在/phpcms/modules/member/in ...
- bzoj 5289: [Hnoi2018]排列
Description Solution 首先注意到实际上约束关系构成了一棵树 考虑这个排列 \(p\),编号为 \(a[i]\) 的出现了,\(i\) 才可以出现 那么如果连边 \((a[i],i) ...
- hdu 5750 Dertouzos 素数
Dertouzos Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- Codeforces 2B. The least round way
There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a w ...
- 作为开发也要了解的 mysql 优化思路
作为开发人员,数据库知识掌握的可能不是很深入,但是一些基本的技能还是要有时间学习一下的.作为一个数据库菜鸟,厚着脸皮来总结一下 mysql 的基本的不能再基本的优化方法. 为了更好的说明,我假想出来了 ...
- Linux之grep命令
概述 所有的类linux系统都会提供一个名为grep(global regular expression print,全局正则表达式输出)的搜索工具.grep命令在对一个或多个文件的内容进行基于模式的 ...