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

本文地址: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. 第3/24周 区_SQL Server中管理空间的基本单位

    哇哦,SQL Server性能调优培训已经进入第3周了!同时你已经对SQL Server内核运行机制有了很好的认识.今天我会讲下SQL Server中的区管理,因为这是个很重要的话题,我们会在第23周 ...

  2. DedeCMS学习

    也许有些读者并不了解dedecms,这里简单介绍一下:DedeCMS是一个自由和开放源码的内容管理系统,它是一个可以独立使用的内容发布系统(CMS).织梦内容管理系统(DedeCms) 以简单.实用. ...

  3. 使用archiver在nodejs下打包

    archiver是一个在nodejs中能跨平台实现打包功能的模块,可以打zip和tar包,是一个比较好用的三方模块. 使用前先安装archiver模块. npm install archiver 建立 ...

  4. 使用ExpandoObject来实现多个Model传送至视图

    昨天Insus.NET有实现<使用ViewModel来实现多个Model传送至视图>http://www.cnblogs.com/insus/p/5594134.html 那今天Insus ...

  5. WinForm 简单蒙版实现控件遮盖

    在Web上面要实现一个遮罩层或者说是蒙版吧,有了DIV那不算什么难事,只要给div定好位置和大小,把颜色的Alpha值设一下就有透明的效果.不过在Winform中实现起来就没那么简单了事.尝试过用一个 ...

  6. 一些sql三

    1.1=1,1=2的使用,在SQL语句组合时用的较多 “where 1=1” 是表示选择全部    “where 1=2”全部不选,如:if @strWhere !='' beginset @strS ...

  7. cefglue埋坑记录

    很少写博客,写的不好,请多多包含,主要是记录工作中的一些问题,和园子里朋友一起讨论学习. 写埋坑记录之前,我先介绍下为什么会使用这个webkit内核的浏览器组件,我是wpf项目使用富文本编辑器,话说w ...

  8. [修复] Firemonkey 画线问题(Android & iOS 平台)

    问题:官方 QC 的一个 Firemonkey 移动平台画线问题: RSP-14309: [iOS & Android] Delphi 10.1 Berlin - drawing proble ...

  9. 11.21 if条件语句 年月日执行判断

    <script language="javascript"> var nian=prompt("输入一个年份","") if(n ...

  10. Scalaz(14)- Monad:函数组合-Kleisli to Reader

    Monad Reader就是一种函数的组合.在scalaz里函数(function)本身就是Monad,自然也就是Functor和applicative.我们可以用Monadic方法进行函数组合: i ...