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

本文地址: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" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp" ><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自动更新设置"android:textColor="#000"android:textSize="18sp" /><TextViewandroid: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" /><CheckBoxandroid:id="@+id/cb_box"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true" /><Viewandroid: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" ><TextViewstyle="@style/TitleStyle"android:text="设置中心" /><!--<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp" ><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自动更新设置"android:textColor="#000"android:textSize="18sp" /><TextViewandroid: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" /><CheckBoxandroid: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.SettingItemViewandroid:layout_width="match_parent"android:layout_height="wrap_content" ></com.wuyudong.mobilesafe.view.SettingItemView></LinearLayout>

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

Android 手机卫士--自定义组合控件构件布局结构相关推荐

  1. Android 自定义组合控件小结

    Android 自定义组合控件小结 引言 接触Android UI开发的这段时间以来,对自定义组合控件有了一定的了解,为此小结一下,本文小结内容主要讨论的是如何使用Android SDK提供的布局和控 ...

  2. Android自定义组合控件--EditText和Button组合成带有清空EditText内容功能的复合控件

    目标:实现EditText和Button组合成带有清空EditText内容功能的复合控件,可以通过代码设置自定义控件的相关属性. 实现效果为: (1)在res/layout目录下编写自定义组合控件的布 ...

  3. Android View体系(十)自定义组合控件

    相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...

  4. Android自定义组合布局,Android 流式布局 + 自定义组合控件

    自定义组合控件 package yanjupeng.bawei.com.day09.two; import android.content.Context; import android.util.A ...

  5. Android Studio自定义组合控件

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

  6. Android自定义View 自定义组合控件

    自定义组合控件: 以三国杀游戏武将为例,包括武将头像,血条,装备区 1.先定义该组合的XML文件布局 1 <?xml version="1.0" encoding=" ...

  7. 自定义组合控件:下拉选择框

    Spinner 自定义组合控件之下拉选择框 项目概述 下拉选择框主要是通过在EditText 下用PopupWindow 动态显示ListView 控件来实现的.下拉选择框可以方便用户的输入效率,以此 ...

  8. 自定义组合控件:Banner、轮播图、广告栏控件

    1. 项目概述 这里,我们使用自定义组合控件实现一个自动轮播的广告条,也叫轮播图,完整版的效果图如下图所示.其实,这就是我们经常见到的滚动广告,默认情况下每隔N 秒会自动滚动,用手指左右滑动时也会切换 ...

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

    为了能让代码能够更多的复用,故使用组合控件.下面是我正在写的项目中用到的方法. 1.先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中. <?xml version="1. ...

最新文章

  1. UA MATH636 信息论5 信道编码简介
  2. 1gb 云服务器 和2gb的区别_(12)虚拟主机/VPS/云主机/服务器有什么区别?
  3. Linux学习总结(56)——如何处理阿里云服务器上入侵的kdevtmpfsi 挖矿病毒
  4. ASP.Net学习笔记008--ASP.Net揭秘之Input版自增补充说明
  5. 安装apache 后,找不到服务,解决办法
  6. 网格自适应_网格自适应的 2 种方法——实现更高效的计算
  7. UltraISO9.3.0.2610中文绿色注册版
  8. 【数字逻辑】学习笔记 第三章 Part2 逻辑函数的化简
  9. El Capitan/Serial on the Intel Skylake NUC
  10. win7系统调整屏幕刷新率方法
  11. 手把手教你搭建一个OPCDA/UA服务器
  12. Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器!跨平台支持Win/Mac/Linux,支持32与64位,支持各种流行编程语言的语法高亮、代码补全等...
  13. cryptsetup 使用说明 中文翻译
  14. 感时花溅泪,恨别鸟惊心
  15. Ajax 前后端数据交互
  16. 从2014全球出版业50强排行榜看出版产业的格局演变
  17. 离散实验3 集合的基本运算 (编程要求 根据提示,练习集合的基本运算法则)
  18. Java 后端工程师 2018 书单推荐
  19. 这家公司把裁员写进OKR,脸都不要了
  20. js 回车与空格转义

热门文章

  1. 一步步实现 Redis 搜索引擎
  2. MyBatis 和 Spring 中的 23 种设计模式,真香,拿去用吧!
  3. 为什么要放弃 Lombok ?
  4. 干掉Navicat:这个IDEA的兄弟真香!
  5. 东半球最接地气的短链接系统设计
  6. SpringBoot第十四篇:在springboot中用redis实现消息队列
  7. 在家搭建大数据分布式计算环境!
  8. 11/1787, 哈工大小学妹的比赛上分经验,附战友招募
  9. 这是一份非常全面的开源数据集!
  10. 我18岁辍学,22岁进谷歌,还需要本科文凭吗?