Android开发之自定义组合控件
自定义组合控件的步骤
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开发之自定义组合控件的更多相关文章
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- Android自定义控件之自定义组合控件(三)
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- Android 手机卫士--自定义组合控件构件布局结构
由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...
- [android] 手机卫士自定义组合控件
设置中心 新建SettingActivity 设置GridView条目的点击事件 调用GridView对象的setOnItemClickListenner()方法,参数:OnItemClickList ...
- android自定义控件(五) 自定义组合控件
转自http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html 代码为自己编写 目标:实现textview和ImageButton组合, ...
- Android开发学习笔记-自定义组合控件的过程
自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...
- Android Studio自定义组合控件
在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...
- Android自定义组合控件详细示例 (附完整源码)
在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...
- Android中自定义组合控件
Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...
随机推荐
- 浏览器页面区域大小的js获取方法
浏览器页面区域大小的获取: /在IE.FireFox.Opera下都可以使用 document.body.clientWidth document.body.clientHeight //即可 ...
- SendMessage 窗口函数
函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.而函数PostMessage不同,将一个消息寄送到一个线程的消息队列后立即返回. MSD ...
- SQL Server— 存在检测、建库、 建表、约束、外键、级联删除
/******************************************************************************** *主题: SQL Server- 存 ...
- ASP.NET 学习笔记(一)ASP.NET 概览
ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式:Web Pages(Web 页面).MVC(Mode ...
- Django设置
运行 django-admin.py startproject [project-name] 命令会生成一系列文件,在Django 1.6版本以后的 settings.py 文件中有以下语句: # B ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- Hello World for U (20)
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...
- 微软职位内部推荐-SDE II-MODC-Beijing
微软近期Open的职位: JOB TITLE: Software Design Engineer IIDEPARTMENT: Microsoft Office Division ChinaIMMEDI ...
- Webstorm10.0.4注册码
分享几个Webstorm10的注册码: (1) user name :EMBRACE ===== LICENSE BEGIN =====17739-1204201000002KkN!4z2O8JEyj ...
- C#中结构体与字节流互相转换
1.定义与C++对应的C#结构体 在c#中的结构体不能定义指针,不能定义字符数组,只能在里面定义字符数组的引用. C++的消息结构体如下: //消息格式 4+16+4+4= 28个字节 struct ...