转自http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html 代码为自己编写

目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性。

通过代码或者通过xml设置自定义控件的属性

1.控件布局:以Linearlayout为根布局,一个TextView,一个ImageButton。 
  

Xml代码
  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继承: 
  

Java代码
  1. public class ImageBtnWithText extends LinearLayout {
  2.  }
  3.   public ImageBtnWithText(Context context) {
  4.   this(context, null);
  5.   }
  6.  
  7.   public ImageBtnWithText(Context context, AttributeSet attrs) {
  8.   super(context, attrs);
  9. //在构造函数中将Xml中定义的布局解析出来。
  10.   LayoutInflater.from(context).inflate(R.layout.imagebtn_with_text, this, true);
  11. }
  12.   }

3.在主界面布局xml中使用自定义控件: 
  

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代码设置,在控件代码中提供函数接口: 
  

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

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

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中定义的属性 
  

Java代码
  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定义属性名 
  

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

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

Xml代码
  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>

效果图

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

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

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

  2. Android自定义控件之自定义组合控件(三)

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

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

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

  4. Android开发之自定义组合控件

    自定义组合控件的步骤1.自定义一个View,继承ViewGroup,比如RelativeLayout2.编写组合控件的布局文件,在自定义的view中加载(使用View.inflate())3.自定义属 ...

  5. [android] 手机卫士自定义组合控件

    设置中心 新建SettingActivity 设置GridView条目的点击事件 调用GridView对象的setOnItemClickListenner()方法,参数:OnItemClickList ...

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

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

  7. Android开发学习笔记-自定义组合控件的过程

    自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...

  8. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  9. Android View体系(十)自定义组合控件

    相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...

随机推荐

  1. jenkins 构建一个前端web项目

    Jenkins发布web前端代码 “系统管理”“管理插件”“已安装” 检查是否有“Git plugin”和“Publish Over SSH”两个插件,如果没有,则需点击“可选插件”,找到它并安装 ...

  2. java中的值传递和引用传递区别

    值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...

  3. linux下编译ffmpeg 引入外部库x264

    Found no assembler Minimum version is nasm-2.13 If you really want to compile without asm, configure ...

  4. Rate Monotonic Scheduling algorithm

    这篇文章写得不错 http://barrgroup.com/embedded-systems/How-To/RMA-Rate-Monotonic-Algorithm 另外rtems的官方文档也有类似说 ...

  5. SQL Server中排名函数row_number,rank,dense_rank,ntile详解

    SQL Server中排名函数row_number,rank,dense_rank,ntile详解 从SQL SERVER2005开始,SQL SERVER新增了四个排名函数,分别如下:1.row_n ...

  6. 近期公共祖先(LCA)——离线Tarjan算法+并查集优化

    一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...

  7. Spring利用propertyConfigurer类 读取.property数据库配置文件

    (1).基本的使用方法是: <bean id="propertyConfigurerForAnalysis" class="org.springframework. ...

  8. eclipse没有(添加)"Dynamic Web Project"选项的方法

    建议使用代理lantern,否则可能要花很长时间显示和下载插件 http://www.dabu.info/eclipse-no-add-dynamic-web-project-option.html ...

  9. 创业做移动互联网App的4个注意事项

    导语:大多数人对于做App还是比較盲目,有个想法立刻就去做了.做出来了才忽然想到市场和推广.我把做移动 互联网App注意事项情给大家列下. 文| 移动互联网李建华 近 来,常常有人问我关于推广的事情, ...

  10. Android 异常解决方法【汇总】

    (1)异常:Android中引入第三方Jar包的方法(Java.lang.NoClassDefFoundError解决办法) 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方 ...