Android中的自定义视图控件
简介
当现有控件不能满足需求时,就需要自定义控件。
自定义控件属性
自定义控件首先要继承自View,重写两个构造函数。
第一个是代码中使用的:
public MyRect(Context context) {
super(context);
}
另一个是资源解析程序使用的:
public MyRect(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);
int color = ta.getColor(R.styleable.MyView_rect_color, 0xff00ffff);
setBackgroundColor(color);
ta.recycle();
}
也可以给自定义控件指定属性,创建attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="MyView">
<attr name="rect_color" format="color"/>
</declare-styleable> </resources>
然后就可以在layout文件中使用了:
<com.wanxiang.www.learncustomview.MyRect
android:id="@+id/myrect"
android:layout_width="100dp"
android:layout_height="100dp"
jkxy:rect_color="#FF000FFF"/>
自定义控件皮肤
可以给button等控件通过background属性设置背景,并根据控件的状态做出改变。定义background为一个xml:
<Button
android:text="Button"
android:background="@drawable/button_skin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"/>
定义这个xml文件内容为:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/btn_normal"></item>
<item android:state_pressed="true" android:drawable="@drawable/btn_pressed"></item>
</selector>
即可以实现控件背景根据状态做出改变。
利用绘图API自定义视图
覆盖draw函数:
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawRect(0,0,500,500,paint); }
private void initproperties() {
paint = new Paint();
paint.setColor(Color.RED);
}
Android中的自定义视图控件的更多相关文章
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- android中一个评分的控件
RatingBar android中一个评分的控件 如何使用 Android Studio下: dependencies { compile 'com.hedgehog.ratingbar:app:1 ...
- Android 手机卫士--自定义组合控件构件布局结构
由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...
- Android开发之自定义组合控件
自定义组合控件的步骤1.自定义一个View,继承ViewGroup,比如RelativeLayout2.编写组合控件的布局文件,在自定义的view中加载(使用View.inflate())3.自定义属 ...
- Android自定义控件之自定义组合控件(三)
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- android自定义控件(五) 自定义组合控件
转自http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html 代码为自己编写 目标:实现textview和ImageButton组合, ...
- Android中使用shape来定义控件
本文章转接于:http://kofi1122.blog.51cto.com/2815761/521605 Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对s ...
- android中的五大布局(控件的容器,可以放button等控件)
一.android中五大布局相当于是容器,这些容器里可以放控件也可以放另一个容器,子控件和布局都需要制定属性. 1.相对布局:RelativeLayout @1控件默认堆叠排列,需要制定控件的相对位置 ...
- [android] 手机卫士自定义组合控件
设置中心 新建SettingActivity 设置GridView条目的点击事件 调用GridView对象的setOnItemClickListenner()方法,参数:OnItemClickList ...
随机推荐
- 图解说明——究竟什么是Windows句柄
图解说明——究竟什么是Windows句柄 参考资料:http://blog.csdn.net/newjerryj/article/details/4383701 http://www.cnblogs. ...
- oracle 开窗分析函数和树形结构
1.row_number() over(partition by ... order by ...)2.rank() over(partition by ... order by ...)3.dens ...
- iOS解决NSData转NSString后字符为空
iOS中,将NSData转NSString的一般方法为[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];但是当dat ...
- Obtaining Query Count Without executing a Query in Oracle D2k
Obtaining Query Count Without executing a Query in Oracle D2k Obtaining a count of records that will ...
- 增量处理属性之记录模式(Record Modes)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- iOS - Phone 电话
1.调用电话 1.1 拨打系统电话 调用系统自带的打电话程序,要跳转到打电话程序,打完电话自动跳转回来. 在 iOS9.0 + 系统隐私控制里禁止查询设备中已安装的 App,所以在 iOS9.0 + ...
- String All Methods
1.public char charAt(int index) public class Test{ public static void main(String args[]){ String s= ...
- web应用动态文档技术
动态生成web文档分为服务器动态生成.客户端动态生成 服务器动态生成文档技术主要有: CGI - 公共网关接口,它是一个允许Web服务器与后端程序以及脚本进行通信的标准化接口.通常是web服务器收到一 ...
- Html5浏览器缓存 sessionStorage 与 localStorage
一.sessionStorage: 浏览关闭会话结束就被清除:(不能跨页面) localStorage:永久保存: 二.使用 var storage = window.sessionStorage; ...
- android 单元测试
android studio 2.0创建一个空android moudle后, 会出现两个test目录,其中一个是Instrument Test, 另一个则是Unit Test Unit Test 和 ...