自定义组合控件的步骤
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. 一本JavaEE的案例书

    案例很好.1.网上书店 2.AJAX网页聊天

  2. 转发 PHP 资料(一)

    WebShell隐藏思路.webshell磁盘读写动态检测.webshell沙箱动态检测(2)   作为WebShell检测.CMS修复.WebShell攻防研究学习的第二篇文章 本文旨在研究Webs ...

  3. python基础教程笔记—即时标记(详解)

    最近一直在学习python,语法部分差不多看完了,想写一写python基础教程后面的第一个项目.因为我在网上看到的别人的博客讲解都并不是特别详细,仅仅是贴一下代码,书上内容照搬一下,对于当时刚学习py ...

  4. size()函数的使用

    matlab中对于矩阵的计算是十分方便的,其中关于矩阵的函数有很多 size() 在c/c++中sizeof用来求某变量所占用的字节数,在matlab中size()则可以用来求矩阵的“长度”,矩阵的长 ...

  5. 2016 系统设计第一期 (档案一)MVC 相关控件整理

    说明:前者是MVC,后者是boostrap 1.form 表单 @using (Html.BeginForm("Create", "User", FormMet ...

  6. EXTJS 4.2 资料 跨域的问题

    关于跨域,在项目开发中难免会遇到:之前笔者是用EXTJS3.0开发项目的,在开发过程中遇到了关于跨域的问题,但是在网上找到资料大部分都是ExtJs4.0以上版本的 在ExtJs中 例如:Ext.Aja ...

  7. c#面向对象机制的进一步理解

    今天看到一个面试题很有意思: namespace EventTest{ class Program { static void Main(string[] args) { A a = new C(); ...

  8. 解决 Eclipse build workspace 慢,validation javascript 更慢的问题

    鸣谢:http://zuoming.iteye.com/blog/1430925 ------------------------------------------------ 如果用到js插件或者 ...

  9. iOS 屏幕旋转 nav+tabbar+present(网页) 2016

    如题,最近一个app架构为 nav + tabbar ,需求是 在点击tabbar中的一个菜单项时,弹出网页,该网页需要横屏显示,其他页面不变  都保持竖屏. XCode Version 7.2.1 ...

  10. 如何循环遍历document.querySelectorAll()方法返回的结果

    使用JavaScript的forEach方法,我们可以轻松的循环一个数组,但如果你认为document.querySelectorAll()方法返回的应该是个数组,而使用forEach循环它: /* ...