Android自己定义控件而且使其能够在xml中自己定义属性
为什么要自己定义View
android开发中自己定义View的优点是显而易见的。比方说以下的这个顶部导航,它被设计出如今应用的每一个界面,但每次的内容却不尽同样。我们不能在每一个layout资源中都配置一组同样的View吧?假设使用<include layou="@layout/xxx"/>标签,尽管攻克了布局文件的重用性,可是相关View的初始化设置还是没可以重用(集中),须要每次都採用view.findViewById(id)来初始化他们。
有了对“可重用性”的考量,我们来完毕一次对自己定义View的探索。
第一步,创建自己定义View的布局文件
这里创建的布局文件和平日里为Activity或Fragment创建的布局文件没有差别,比如以下的xml创建了一个上面是图片,以下是文字的自己定义View布局:
<?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" > <ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_launcher" /> <TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="asdfasdf" /> </LinearLayout>
第二步,创建继承自View或其子类的自己定义View
package org.xiaom.customView.view; import org.xiaom.customView.R; public class MyView extends LinearLayout {
private View root = null;
//上面的img
private ImageView imgView = null;
//img以下的text
private TextView txtView = null; public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
} public MyView(Context context) {
super(context);
initView(context);
} private void initView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root = inflater.inflate(R.layout.view_my_view, this,true);
imgView = (ImageView) root.findViewById(R.id.img);
txtView = (TextView) root.findViewById(R.id.txt);
} }
第三步,在xml中配置、使用自己定义View
在完毕上面的自己定义操作后,接下来就能够像使用android原生组件那样使用我们的自己定义View了。须要注意的是,自己定义View在xml中声明使用时,必须採用全类名(类全名- -)。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 类全名 -->
<org.xiaom.customView.view.MyView
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="aasdfasdfasdfa" /> </LinearLayout>
接下来就非常easy了,我们直接使用Activity.this.setContentView(layouId)方法就可以。
第四步,配置declare-styleable以声明自己定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="myView">
<attr name="img" format="reference" />
<attr name="text" format="string"></attr>
</declare-styleable>
</resources>
第五步,配置自己定义属性并读取、应用
package org.xiaom.customView.view; import org.xiaom.customView.R; public class MyView extends LinearLayout {
private View root = null;
// 上面的img
private ImageView imgView = null;
// img以下的text
private TextView txtView = null;
// 上面的图像资源Id
Drawable img;
// 文字内容
String text; public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.myView);
img = ta.getDrawable(R.styleable.myView_img);
text = ta.getString(R.styleable.myView_text);
initView(context);
//记得此处要recycle();
ta.recycle();
} private void initView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root = inflater.inflate(R.layout.view_my_view, this, true);
imgView = (ImageView) root.findViewById(R.id.img);
txtView = (TextView) root.findViewById(R.id.txt);
//将自己定义属性的值传递给相应View
imgView.setBackgroundResource(R.drawable.icon_consultation);
txtView.setText(text);
} }
以下的xml表明怎样在xml中配置自己定义属性:
<!-- 我的adt版本号是v22.6.2-1085508,它支持自己主动找寻和验证declare-styleable;假设你的adt不支持此功能,请换成 -->
<!-- xmlns:myView="http://schemas.android.com/apk/org.xiaom.customView.myView",此处我没验证 --> <LinearLayout xmlns:myView="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <!-- 类全名 --> <org.xiaom.customView.view.MyView
android:layout_width="match_parent"
android:layout_height="wrap_content"
myView:img="@drawable/icon_consultation"
myView:text="自己定义Text" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="aasdfasdfasdfa" /> </LinearLayout>
好,大功告成。这里是本博文实例的Eclipseproject。
Android自己定义控件而且使其能够在xml中自己定义属性的更多相关文章
- Winform下让你的DataGridView控件支持点语法(即显示list中的子对象属性)
前言: 不想看前言的直接去看正文吧!另外文末有彩蛋. DataGridView可以支持多种数据源格式,比如DataTable和List. DataTable没啥特殊的,本身就是一张二维的表,可以和Da ...
- Android应用之——自己定义控件ToggleButton
我们经常会看到非常多优秀的app上面都有一些非常美丽的控件,用户体验非常好.比方togglebutton就是一个非常好的样例,IOS系统以下那个精致的togglebutton现在在android以下也 ...
- Android自己定义控件之应用程序首页轮播图
如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...
- Android自己定义控件(状态提示图表)
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...
- Android自己定义控件系列五:自己定义绚丽水波纹效果
尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...
- Android自己定义控件系列案例【五】
案例效果: 案例分析: 在开发银行相关client的时候或者开发在线支付相关client的时候常常要求用户绑定银行卡,当中银行卡号一般须要空格分隔显示.最常见的就是每4位数以空格进行分隔.以方便用户实 ...
- 【Asp.net之旅】--因自己定义控件注冊而引发的思考
前言 近期在开发远洋的SOA系统平台,开发使用的是.NET平台.对于Asp.net并不困难,但该系统的开发并非全然依靠Asp.net.而是自身封装好的框架.这套框架是远洋地产购买的微软的开发平台,项目 ...
- 【Android】自己定义控件——仿天猫Indicator
今天来说说类似天猫的Banner中的小圆点是怎么做的(图中绿圈部分) 在学习自己定义控件之前,我用的是很二的方法,直接在布局中放入多个ImageView,然后代码中依据Pager切换来改变图片.这样的 ...
- 【Android】自己定义控件实现可滑动的开关(switch)
~转载请注明来源:http://blog.csdn.net/u013015161/article/details/46704745 介绍 昨天晚上写了一个Android的滑动开关, 即SlideSwi ...
随机推荐
- 如何在win7上面安装python的包
最近在win7上面搞python,然后写的一些代码涉及到了对Excel的读写.所以需要用到包xlrd xlwt xlutils. 但问题是这些包import后显示的是找不到.错误提示是:Import ...
- 2016021903 - 下载安装使用Memory Analyzer
Memory Analyzer是做什么的? 分析java程序中分析内存泄露问题. 1.下载Memory Analyzer Memory Analyzer下载地址:http://www.eclipse. ...
- 关于scroll无法绑定的问题
关于jQuery的scroll([[data],fn])事件, 概述是:当用户滚动指定的元素(元素包括:所有可滚动的元素和 window 对象)时,会触发该事件. 如绑定窗口对象: $(window) ...
- bzoj 1004 1004: [HNOI2008]Cards burnside定理
1004: [HNOI2008]Cards Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1668 Solved: 978[Submit][Stat ...
- JdbcTemplate 操作Oracle Blob
1:增加操作 public int addTest(TestVo tv) { byte bz[] = tv.getBz().getBytes(); LobHandler lobHandler = ne ...
- Windows Azure 配置SSTP
方法參考下面文章的步驟. 这个成功.http://freevpnba.com/windows-azure-sstp-vpn/ 这个没成功,不知道为什么.http://diaosbook.com/pos ...
- 【UVA10765】Doves and bombs (BCC求割点后联通块数量)
题目: 题意: 给了一个联通无向图,现在问去掉某个点,会让图变成几个联通块? 输出的按分出的从多到小,若相等,输出标号从小到大.输出M个. 分析: BCC求割点后联通块数量,Tarjan算法. 联通块 ...
- java桌面项目打包_by icewee_写得太棒了,直接转载了
前言: 我们都知道Java可以将二进制程序打包成可执行jar文件,双击这个jar和双击exe效果是一样一样的,但感觉还是不同.其实将java程序打包成exe也需要这个可执行jar文件. 准备: ecl ...
- VS2010 MFC GDI+ 实现PNG透明图片显示
网上找了一些资料学习了一下PNG图的显示,这里总结一下. 参考:http://blog.csdn.net/czyt1988/article/details/7965066 一.VS2010配置GDI+ ...
- HDU 3045 Picnic Cows(斜率优化DP)
Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...