自定义组合控件的步骤
1.自定义一个View,继承ViewGroup,比如RelativeLayout
2.编写组合控件的布局文件,在自定义的view中加载
(使用View.inflate())
3.自定义属性,
在value中创建一个attrs.xml文件,定义自己的属性
4.在自定义View的java代码中完成逻辑
5.在使用自定义view的布局文件中,添加自己的命名空间,在自定义view中,手动添加自己定义的属性

如:

1.自定义一个View,继承ViewGroup。命名为:setting_item_view.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:padding="5dp" > <TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="22sp" /> <TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginTop="3dp"
android:textColor="@color/gray"
android:textSize="18sp" /> <CheckBox
android:id="@+id/cb_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" /> <View
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:layout_alignParentBottom="true"
android:background="@color/gray" /> </RelativeLayout>

2.编写组合控件的布局文件,在自定义的view中加载。命名:SettingItemView.java

 import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView; public class SettingItemView extends RelativeLayout { public SettingItemView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initview();
} public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initview();
} public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initview();
} public SettingItemView(Context context) {
super(context);
initview();
} private void initview() {
View.inflate(getContext(), R.layout.setting_item_view, this); }

3.在value中创建一个attrs.xml文件,定义自己的属性,title, update_on, update_off

 <?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="SettingItemView">
<attr name="title" format="string" />
<attr name="update_on" format="string" />
<attr name="update_off" format="string" />
</declare-styleable> </resources>

4.在自定义View的java代码中完成逻辑,SettingItemView.java

 import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView; public class SettingItemView extends RelativeLayout {
// 命名空间,与布局文件中写的命名空间保持一致
private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.haha.mobilesafe";
private TextView tv_title;
private TextView tv_desc;
private CheckBox cb_status;
private String mTitle;
private String mUpdateOn;
private String mUpdateOff; public SettingItemView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initview();
} public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initview();
} public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
mTitle = attrs.getAttributeValue(NAMESPACE, "title");
mUpdateOn = attrs.getAttributeValue(NAMESPACE, "update_on");
mUpdateOff = attrs.getAttributeValue(NAMESPACE, "update_off"); initview();
} public SettingItemView(Context context) {
super(context);
initview();
} private void initview() {
View.inflate(getContext(), R.layout.setting_item_view, this);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_desc = (TextView) findViewById(R.id.tv_desc);
cb_status = (CheckBox) findViewById(R.id.cb_status);
setTitle(mTitle);
} // 设置item的名称
public void setTitle(String text) {
tv_title.setText(text);
} // 设置item的描述
public void setDesc(String text) {
tv_desc.setText(text);
} // 获取checkbox是否勾选
public boolean isChecked() {
return cb_status.isChecked();
} // 设置checkbox勾选状态
public void setChecked(boolean check) {
if (check) {
setDesc(mUpdateOn);
} else {
setDesc(mUpdateOff);
}
cb_status.setChecked(check);
} }

5.在使用自定义view的布局文件中,添加自己的命名空间,在自定义view中,手动添加自定义的属性

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:haha="http://schemas.android.com/apk/res/com.haha.mobilesafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
style="@style/titlebar"
android:text="设置中心" /> <com.haha.mobilesafe.view.SettingItemView
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
haha:title="自动更新设置"
haha:update_off="自动更新已关闭"
haha:update_on="自动更新已开启" /> </LinearLayout>

Android开发之自定义组合控件的更多相关文章

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

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

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

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

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

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

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

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

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

    转自http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html 代码为自己编写 目标:实现textview和ImageButton组合, ...

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

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

  7. Android Studio自定义组合控件

    在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...

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

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

  9. Android中自定义组合控件

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

随机推荐

  1. python字典根据value排序

    参考: http://docs.python.org/2/howto/sorting.html http://www.cnpythoner.com/post/266.html http://ghost ...

  2. (转载)直接用SQL语句把DBF导入SQLServer

    告诉大家一个直接用SQL语句把DBF导入SQLServer,以及txt导入Access的方法,大家抛弃BatchMove吧来自:碧血剑告诉你一个最快的方法,用SQLServer连接DBF在SQLSer ...

  3. SqlServer中创建Oracle连接服务器

    转自太祖元年的:http://www.cnblogs.com/jirglt/archive/2012/06/10/2544025.html参考:http://down.51cto.com/data/9 ...

  4. fedora 解决yumBackend.py进程CPU占用过高

    fedora启动时电脑风扇噪声巨响,检查进行发现是yumBackend.py进行占用CPU过高. yumBackend.py进行是后台检查更新,如果觉得没用可以使用工具关闭检查更新,或者修改检查周期. ...

  5. 深入浅出话XAML-学习笔记

    第一章 XAML是什么? 1.1XAML之前 *设计师的设计更不上程序逻辑的变化 *程序员未能完全实现设计师提供的效果图 *效果图与程序功能不能完全匹配 *从效果图到软件UI的转化耗费很多时间 1.2 ...

  6. EXTJS4.2 chart 柱状图

    chart 柱状图 Ext.require('Ext.chart.*'); Ext.require(['Ext.Window', 'Ext.fx.target.Sprite', 'Ext.layout ...

  7. 浏览器对象模型BOM(Browser Object Model)

    1.结构 BOM是Browser Object Model的缩写,简称浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 由于BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象是w ...

  8. hdu 4717 The Moving Points(第一个三分题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4717 [题意]: 给N个点,给出N个点的方向和移动速度,求每个时刻N个点中任意两点的最大值中的最小值,以及取最小 ...

  9. mysql建表且某字段内不允许出现重复值

    CREATE TABLE `admin` ( `id` ) NOT NULL AUTO_INCREMENT , `username` varchar() NOT NULL , `password` v ...

  10. java对象数组

    问题描述:     java 对象数组的使用 问题解决: 数组元素可以是任何类型(只要所有元素具有相同的类型) 数组元素可以是基本数据类型 数组元素也可以是类对象,称这样的数组为对象数组.在这种情况下 ...