原文地址:http://mypyg.iteye.com/blog/968646

目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性。 
1.控件布局:以Linearlayout为根布局,一个TextView,一个ImageButton。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent" android:layout_height="fill_parent"
  4. android:gravity="center_vertical">
  5. <TextView android:layout_height="wrap_content" android:id="@+id/text1"
  6. android:layout_width="wrap_content"></TextView>
  7. <ImageButton android:layout_width="wrap_content"
  8. android:layout_height="wrap_content" android:id="@+id/btn1"></ImageButton>
  9. </LinearLayout>

2.自定义控件代码,从LinearLayout继承:

  1. public class ImageBtnWithText extends LinearLayout {
  2. public ImageBtnWithText(Context context) {
  3. this(context, null);
  4. }
  5. public ImageBtnWithText(Context context, AttributeSet attrs) {
  6. super(context, attrs);
  7. LayoutInflater.from(context).inflate(R.layout.imagebtn_with_text, this, true);
  8. }
  9. }

在构造函数中将Xml中定义的布局解析出来。 
PS:有时执行此句代码时eclipse会输出找不到资源,而无法预览自定义控件。经试验确定是新增的布局没有生成资源,但是clean project重新生成也不行,最后重启eclipse解决。 
3.在主界面布局xml中使用自定义控件:

  1. <com.demo.widget2.ImageBtnWithText
  2. android:id="@+id/widget"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" />

即使用完整的自定义控件类路径:com.demo.widget2.ImageBtnWithText定义一个元素。 
运行可以看到控件已经能够被加载到界面上。 
4.给按钮设置图片和文本 
如果图片是固定不变的,那么直接在控件布局中设置ImageButton的src属性即可。 
4.1通过Java代码设置,在控件代码中提供函数接口:

  1. public void setButtonImageResource(int resId) {
  2. mBtn.setImageResource(resId);
  3. }
  4. public void setTextViewText(String text) {
  5. mTv.setText(text);
  6. }

然后再在主界面的onCreate()通过函数调用设置即可。 
4.2通过Xml设置属性 
4.2.1首先定义Xml可以设置的属性集合,在values下创建attrs.xml,文件名可随意,一般都叫attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="ImageBtnWithText">
  4. <attr name="android:text"/>
  5. <attr name="android:src"/>
  6. </declare-styleable>
  7. </resources

属性集合名字:ImageBtnWithText,自己可根据实际来定义; 
集合中包含的属性列表,每行一个属性。 
4.2.2修改自定义控件实现代码,以获取xml中定义的属性

  1. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageBtnWithText);
  2. CharSequence text = a.getText(R.styleable.ImageBtnWithText_android_text);
  3. if(text != null) mTv.setText(text);
  4. Drawable drawable = a.getDrawable(R.styleable.ImageBtnWithText_android_src);
  5. if(drawable != null) mBtn.setImageDrawable(drawable);
  6. a.recycle()

首先通过context.obtainStyledAttributes获得所有属性数组; 
然后通过TypedArray类的getXXXX()系列接口获得相应的值。 
4.2.3在主界面布局中设置自定义控件的属 
android:text="ABC" android:src="@drawable/icon 
4.3自定义名称属性: 
在4.2中使用的属性名是android系统命名空间的,都以android开头,比如android:text等。 
实际开发中会自定义一些属性名,这些属性名仍然定义在4.2.1提到的attrs.xml中: 
4.3.1定义属性名

  1. <attr name="appendText" format="string"/>

和直接使用系统的attr不同的是要增加一个format属性,说明此属性是什么格式的。format可选项可参见注1 
4.3.2使用自定义属性

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:myspace="http://schemas.android.com/apk/res/com.demo.customwidget"
  4. android:orientation="vertical" android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <com.demo.widget2.ImageBtnWithText
  7. android:text="ABC" android:src="@drawable/icon" android:id="@+id/widget"
  8. android:layout_width="fill_parent" android:layout_gravity="center"
  9. android:layout_height="fill_parent" myspace:appendText="123456">
  10. </com.demo.widget2.ImageBtnWithText>
  11. </LinearLayout>

直接在主布局文件中使用此属性appendText="abc"是不会设置生效的,而是要在主布局xml中定义一个xml命名空间: 
xmlns:myspace="http://schemas.android.com/apk/res/com.demo.customwidget" 
命名空间的名字可以自己随便定义,此处为myspace,即xmlns:myspace; 
后面的地址则有限制,其开始必须为:"http://schemas.android.com/apk/res/",后面则是包名com.demo.customwidget, 
此处的包名与AndroidManifest.xml中<manifest>节点的属性package="com.demo.customwidget"一致,不是自定义控件Java代码所在的包,当然简单的程序自定义控件Java代码也一般在此包内。

注1: 
注1:format可选项 
"reference" //引用 
"color" //颜色 
"boolean" //布尔值 
"dimension" //尺寸值 
"float" //浮点值 
"integer" //整型值 
"string" //字符串 
"fraction" //百分数,比如200% 
枚举值,格式如下: 
<attr name="orientation"> 
<enum name="horizontal" value="0" /> 
<enum name="vertical" value="1" /> 
</attr>   
xml中使用时: 
android:orientation = "vertical"

标志位,位或运算,格式如下: 
<attr name="windowSoftInputMode"> 
<flag name = "stateUnspecified" value = "0" /> 
<flag name = "stateUnchanged" value = "1" /> 
<flag name = "stateHidden" value = "2" /> 
<flag name = "stateAlwaysHidden" value = "3" /> 
<flag name = "stateVisible" value = "4" /> 
<flag name = "stateAlwaysVisible" value = "5" /> 
<flag name = "adjustUnspecified" value = "0x00" /> 
<flag name = "adjustResize" value = "0x10" /> 
<flag name = "adjustPan" value = "0x20" /> 
<flag name = "adjustNothing" value = "0x30" /> 
</attr>         
xml中使用时: 
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">

另外属性定义时可以指定多种类型值,比如: 
<attr name = "background" format = "reference|color" /> 
xml中使用时: 
android:background = "@drawable/图片ID|#00FF00"

(转)android自定义组合控件的更多相关文章

  1. Android自定义组合控件详细示例 (附完整源码)

    在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...

  2. 014 Android 自定义组合控件

    1.需求介绍 将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用类似布局时,直接使用该组合控件的对象. 优点:可复用. 例如要重复利用以下布局: <RelativeLayout an ...

  3. Android自定义组合控件

    今天和大家分享下组合控件的使用.很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便 ...

  4. Android自定义组合控件内子控件无法显示问题

    今天自定义了一个组合控件,与到了个奇葩问题: 我自定义了一个RelativeLayout,这个layout内有多个子控件.但奇怪的是这些子控件一直显示不出来.调试了一下午,竟然是因为在获取(infla ...

  5. android 自定义组合控件 顶部导航栏

    在软件开发过程中,经常见到,就是APP 的标题栏样式几乎都是一样的,只是文字不同而已,两边图标不同.为了减少重复代码,提高效率, 方便大家使用,我们把标题栏通过组合的方式定义成一个控件. 例下图: 点 ...

  6. Android自定义组合控件:UIScrollLayout(支持界面滑动及左右菜单滑动)

    一.前言: 我之前很早的时候,写过一篇<左右滑出菜单>的文章: http://blog.csdn.net/qingye_love/article/details/8776650 用的是对V ...

  7. Android 自定义组合控件

    1, you need to add this kind of code to the constructors of your custom view which must extend ViewG ...

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

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

  9. Android 手机卫士--自定义组合控件构件布局结构

    由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...

随机推荐

  1. PHP用法

    链接: php编写app接口(一)-JSON方式封装接口数据方法 php 非常有用的高级函数PATH_SEPARATOR常量和set_include_path date_default_timezon ...

  2. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 几十套业务系统集中统一授权管理实现经验分享

    由于这几年互联网电商的快速发展,快递公司也进入了快速发展的绝好快速成长期.随着社会的强劲需求公司的业绩年年攀新高.快速发展的公司都需要有强大的IT信息系统,硬件设备基本上款到了货也可以到了,但是软件系 ...

  3. [转]Mac下配置基于SecurID的Cisco IPSec VPN全攻略(有图)

    来自: http://www.eefocus.com/Kevin/blog/11-09/230878_53c71.html RSA的SecurID长的是这个样子滴: Mac里面,可以设置VPN, 方法 ...

  4. java并发编程学习: 守护线程(Daemon Thread)

    在正式理解这个概念前,先把 守护线程 与 守护进程 这二个极其相似的说法区分开,守护进程通常是为了防止某些应用因各种意外原因退出,而在后台独立运行的系统服务或应用程序. 比如:我们开发了一个邮件发送程 ...

  5. CSS常用浮出层的写法

    点此查看实例展示 是的,我们即将实现的就是以上功能,是不是很生动? 贴上HTML: <div class="poptip"> <span class=" ...

  6. 阻止事件冒泡,阻止默认事件,event.stopPropagation()和event.preventDefault(),return fal的区别

    今天来看看前端的冒泡和事件默认事件如何处理 1.event.stopPropagation()方法 这是阻止事件的冒泡方法,不让事件向documen上蔓延,但是默认事件任然会执行,当你掉用这个方法的时 ...

  7. 吉特仓库管理系统-ORM框架的使用

    最近在园子里面连续看到几篇关于ORM的文章,其中有两个印象比较深刻<<SqliteSugar>>,另外一篇文章是<<我的开发框架之ORM框架>>, 第一 ...

  8. jsonp接口的xss防范

    防范方式也很简单,只要在header里输出类型设置为javascript即可: 1 header('Content-type: text/javascript;charset=utf-8');

  9. 从基层容器类看万变不离其宗的JAVA继承体系

    以容器类为例子,可以观一叶而知秋,看看以前的前辈们是如何处理各种面向对象思想下的继承体系的.读的源代码越多,就越要总结这个继承关系否则读的多也忘得快. 首先摆上一张图片: 看到这张图很多人就慌了,难道 ...

  10. BZOJ4520 [Cqoi2016]K远点对

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...