由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现

本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址。

自定义组合控件

1.将已经编写好的布局文件,抽取到一个类中去做管理,下次还需要使用此布局结构的时候,直接使用组合控件对应的对象.

2.将组合控件的布局,抽取到单独的一个xml中

新建布局文件: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="wrap_content" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" > <TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动更新设置"
android:textColor="#000"
android:textSize="18sp" /> <TextView
android:id="@+id/tv_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:text="自动更新已关闭"
android:textColor="#000"
android:textSize="18sp" /> <CheckBox
android:id="@+id/cb_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" /> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/tv_des"
android:background="#000" />
</RelativeLayout> </RelativeLayout>

3.通过一个单独的类SettingItemView.java,去加载此段布局文件.

package com.wuyudong.mobilesafe.view;

import com.wuyudong.mobilesafe.R;

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 TextView tv_des;
private CheckBox cb_box; public SettingItemView(Context context) {
this(context, null);
} public SettingItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// xml-->view 将设置界面的条目转换成view对象
View.inflate(context, R.layout.setting_item_view, this);
// 等同于以下两行代码
/*
* View view = View.inflate(context, R.layout.setting_item_view, null);
* this.addView(view);
*/ //自定义组合控件中的标题描述
TextView tv_title = (TextView) findViewById(R.id.tv_title);
tv_des = (TextView) findViewById(R.id.tv_des);
cb_box = (CheckBox) findViewById(R.id.cb_box);
} }

这样只需要简单的几行代码就可以完成布局文件的调用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
style="@style/TitleStyle"
android:text="设置中心" /> <!--
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" > <TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动更新设置"
android:textColor="#000"
android:textSize="18sp" /> <TextView
android:id="@+id/tv_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:text="自动更新已关闭"
android:textColor="#000"
android:textSize="18sp" /> <CheckBox
android:id="@+id/cb_box"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:layout_below="@id/tv_des"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="1dp" />
</RelativeLayout>
--> <com.wuyudong.mobilesafe.view.SettingItemView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.wuyudong.mobilesafe.view.SettingItemView> </LinearLayout>

运行项目后,有如下效果:

Android 手机卫士--自定义组合控件构件布局结构的更多相关文章

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

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

  2. [android] 手机卫士自定义滚动控件

    TextView控件设置单行显示 android:singleLine=”true” 设置TextView开始的位置显示省略号,android:ellipsize=”start” 设置滚动属性,and ...

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

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

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

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

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

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

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

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

  7. Android Studio自定义组合控件

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

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

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

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

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

随机推荐

  1. [Java 基础]数组

    数组初始化 定义数组语法格式 定义数组有两种方式,如下两种格式是等价的: int[] a1; int a1[];  注:在C/C++中,不支持第一种格式.但是,推荐使用这种方式,因为这样似乎更合理,声 ...

  2. ansible写一个简单的playbook

    前言 实现的功能很简单,就是通过ansible批量完成某个账户sudo权限的开通或关闭 目录结构 ├── group_vars #放置各种变量的目录,我这里没用 ├── hosts #主机和组配置,默 ...

  3. jQuery1.9及其以上版本中动态元素on绑定事件无效解决方案

    jQuery 1.9/2.0/2.1及其以上版本无法使用live函数了,然而jQuery 1.9及其以上版本提供了on函数来代替.本文讲解了jQuery on函数的使用方法,以及在使用jQuery函数 ...

  4. js限制文本框只可以输入数字

    封装了一下,要用的话直接调用下面getEvent函数即可   function getEvent() { if (document.all) { return window.event; //for ...

  5. 使用MVVM-Sidekick开发Universal App(二)

    上一篇文章已经建立了基本的实体类,并且搞定了多语言的问题,以后在app里用字符串的时候就可以从资源文件中取了.现在继续进行. 一.添加一个页面 CurrencyExchanger首页是一个货币兑换的列 ...

  6. MVC 创建线程内的db单例

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using LSUnion.S ...

  7. hhvm的正确安装姿势 http://dl.hhvm.com 镜像

    hhvm是php的第三方运行环境,由facebook出品,基于该运行环境,它还提供了一种编程语言hack - PHP的静态类型版. 折腾了一天后,包括各种编译.配置.FQ,后面终于忍不住搜了一下 ht ...

  8. 市面上常见的javaEE WEB服务软件

    常见的市面上web服务软件 Tomcat:轻量级的WEB应用程序服务器(开源),开源组织Apache的产品.免费的.支持部分的JavaEE规范.(servlet.jsp.jdbc,但ejb, rmi不 ...

  9. python验证登录

    一个web2.0时代的网站,自然少不了用户注册,登录,验证的功能,那么python可以怎样实现登录验证呢 这里我们使用装饰器来做登录验证 网站构成 假设我们有这样一个网站,是一个类似与博客园这种多个用 ...

  10. 软件项目发展历史<人月神话>这本书好

    几乎是计算机软件开发的发展历史     人月神话,增加人手并不一定能提高开发速度. 原因在于,有些任务是无法分解的,存在先后顺序.无法同步进行. 增加人手,增加的是沟通成本,相互牵制.可以分解的任务就 ...