android自定义控件(五) 自定义组合控件

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

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

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

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

Xml代码

[html] view plaincopy

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

[java] view plaincopy

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

[html] view plaincopy

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

[java] view plaincopy

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

[html] view plaincopy

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

[java] view plaincopy

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

[html] view plaincopy

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

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

Xml代码

[html] view plaincopy

  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>

效果图

下载地址 http://download.csdn.net/detail/ethan_xue/4109870

时间: 2024-12-23 14:31:40

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

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

前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发成本,以及维护成本. 使用自定义组合控件的好处? 我们在项目开发中经常会遇见很多相似或者相同的布局,比如APP的标题栏,我们从三种方式实现标题栏来对比自定义组件带来的好处,毕竟好的东西还是以提高开发效率,降低开发成本为导向的. 1.)第一种方式:直接在每个xml布局中写相同的标题栏布局代码 <?xm

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

由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. 自定义组合控件 1.将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用此布局结构的时候,直接使用组合控件对应的对象. 2.将组合控件的布局,抽取到单独的一个xml中 新建布局文件:setting_item_view.xml,将上篇文章中布局文件中的代码放进去 <?xml version=

第十六天 自定义控件和自定义组合控件

1.  setContentView() 一旦调用,layout 会立即显示UI 2. inflate 只会将layout 形成一个以view类 实现 的对象 ,需要显示的时候还需要调用 setContentView() . ---------------------------------------------------------------------------------------------- 自定义控件组合 第一步 :先写要组合的一些需要的控件,将其封装到一个布局xml布局文

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

设置中心 新建SettingActivity 设置GridView条目的点击事件 调用GridView对象的setOnItemClickListenner()方法,参数:OnItemClickListenner对象 匿名内部类实现,重写onItemClick()方法,传递进来的参数: parent是GridView对象,view是当前View对象,position是当前索引 switch判断,当时设置中心的索引时,跳转到设置中心 设置中心界面 使用相对布局,右边的<CheckBox/> 位于父

Android自定义控件——自定义组合控件

转载请注明出处http://blog.csdn.net/allen315410/article/details/39581055  前面几篇博文介绍了Android如何自定义控件,其实就是讲一下如何"从无到有"的自定义一个全新的控件,继承View或者继承ViewGroup,复写其相关方法,这种自定义控件的方式相对来说难度较大,而且并不是所有需要新控件的情况下,都要这样进行.有很多情况下,我们只要运用好Android给我提供好的控件,经过布局巧妙的结合在一起,就是一个新的控件,我称之为&

自定义组合控件和在自定义控件中使用自定义属性

今天,整理了一下我平时的笔记,写一个比较简单的自定义组合控件,仅供小白参考,大神请绕道,希望能够对大家有一些帮助 首先,得明白为什么我们需要自定义组合控件,它是因为原有控件并不能满足开发的需求,或者说并不能达到我们想要的一种效果,这个时候,就需要我们自己定义一些控件,以达到目的 ![先来看一下效果](http://img.blog.csdn.net/20160716224219109) 个人总结自定义控件的步骤: 1.先写一个布局,这里我用的是一个相对布局,我这里的相对布局就是根布局了 <?xm

Android实例-手机安全卫士(七)-自定义组合控件

一.目标. 将多个系统控件(TextView.Button.CheckBox等)组合成一个自定义的控件,并像系统控件一样使用.如图所示第1个自动更新控件是根据相对布局放置而成的,第2个自动更新控件即为自定义组合控件,它可以想一般的TextView等系统控件一样重复使用. 自定义控件如图: 二.代码实现. 1.在layout文件夹下新建一个xml文件(取名model_setting_item.xml),用于保存自定义控件的布局. 2.在新建的xml文件(model_setting_item.xml

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

自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需求,定义一些API方法: ----------------------------------4.根据需要,自定义控件的属性,可以参照TextView属性: 5.自定义命名空间,例如: xmlns:itheima="http://schemas.android.com/apk/res/<包名&

android 自定义组合控件

自定义控件是一些android程序员感觉很难攻破的难点,起码对我来说是这样的,但是我们可以在网上找一些好的博客关于自定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲下自定义组合控件,这个特别适合在标题栏或者设置界面,看下面图: 就非常适合使用组合控件了,现在写一个玩玩: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"