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

本文地址: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. SQL Server代理(11/12):维护计划作业

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在这一系列的上一篇,我们看了使用代理帐户模 ...

  2. IIS各个版本中你需要知道的那些事儿

    一.写在前面 目前市面上所用的IIS版本估计都是>=6.0的.所以我们主要以下面三个版本进行讲解 服务器版本 IIS默认版本 server2003 6.0 server2008 7.0 serv ...

  3. c#中的正则表达式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. Lua使用心得(2)

    在lua脚本调用中,如果我们碰到一种不好的脚本,例如: while 1 do do end 那我们的程序主线程也会被阻塞住.那我们如何防止这种问题呢?下面就给出一个解决的办法. 首先为了不阻塞主线程, ...

  5. 算法实例-C#-快速排序-QuickSort

    算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...

  6. [译]WebVR技术方案草案

    注:基于官方的.bs规范专用格式进行了翻译,但结果发现无法编译成html格式,所幸基本兼容.markdown格式. 中文翻译项目地址:https://github.com/web3d/webvr-sp ...

  7. 我的css笔记

    1.css的使用方法 内嵌样式 <p style="font-size:20pt;color:red;">这个Style定义<p> <!--里面的文字 ...

  8. [moka同学笔记]MySql语句整理

    更改单个表中的字段属性 content 以前为text类型的,现在改为longtext ALTER TABLE `notice` CHANGE `content` `content` LONGTEXT ...

  9. Jquery实际应用,判断radio,selelct,checkbox是否选中及选中的值

    jquery取radio单选按钮的值 $("input[name='items']:checked").val();  另:判断radio是否选中并取得选中的值 如下所示: fun ...

  10. 使用React并做一个简单的to-do-list

    1. 前言 说到React,我从一年之前就开始试着了解并且看了相关的入门教程,而且还买过一本<React:引领未来的用户界面开发框架 >拜读.React的轻量组件化的思想及其virtual ...