在开发应用程序的时候,很多时候会使用到几个重复的控件,例如Android手机的设置界面里面的位置服务里面的每一栏都是组合控件,也就是说多个控件组成一个整体,如下图所示:

红色方框里面的是由两个TextView和一个CheckBox组合而成的一个组合控件,要是能把这两个控件组合成一个控件在开发过程中就有有很大的方便,

在主布局文件中activity_mian:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayout android:layout_width="match_parent"android:layout_height="80dp" ><TextView android:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:layout_marginLeft="10dp"android:textColor="#000000"android:textSize="20sp"android:text="GOOGLE的位置服务"/><TextView android:id="@+id/tv_desc"android:layout_below="@id/tv_title"android:layout_width="280dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:textColor="#88000000"android:textSize="18sp"android:text="允许应用程序使用来自WALN或移动网络的数据确定您的大致位置"/><CheckBox android:id="@+id/cb_status"android:layout_width="wrap_content"android:layout_height="wrap_content"android:clickable="false"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="10dp"/><View android:layout_width="match_parent"android:layout_height="0.2dp"android:layout_below="@id/tv_desc"android:background="#000000"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"/></RelativeLayout></RelativeLayout>

在这里实现这一个单元的控件,但是还有两个单元的控件布局类似,如果还是按照上面的代码往下写,就会造成代码的重复,而且不好看,所以这里就会使用到一个组合控件,很方面的解决这个问题,首先把重复的代码抽取出来

<?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="80dp" ><TextView android:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:layout_marginLeft="10dp"android:textColor="#000000"android:textSize="20sp"android:text="GOOGLE的位置服务"/><TextView android:id="@+id/tv_desc"android:layout_below="@id/tv_title"android:layout_width="280dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:textColor="#88000000"android:textSize="18sp"android:text="允许应用程序使用来自WALN或移动网络的数据确定您的大致位置"/><CheckBox android:id="@+id/cb_status"android:layout_width="wrap_content"android:layout_height="wrap_content"android:clickable="false"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="10dp"/><View android:layout_width="match_parent"android:layout_height="0.2dp"android:layout_below="@id/tv_desc"android:background="#000000"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"/></RelativeLayout>

创建一个自定义的类ItemView.java,该方法继承了RelativeLayout,实现了一下三个方法

public ItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initView(context);}public ItemView(Context context, AttributeSet attrs) {super(context, attrs);initView(context);}public ItemView(Context context) {super(context);initView(context);}

在调用这三个方法时都会对调用initView(context)方法对item_view.xml中的三个控件进行初始化。

<span style="white-space:pre">  </span>// 把一个布局文件----》一个View,并且加载在SettingItemView中private void initView(Context context) {View view = View.inflate(getContext(), R.layout.item_view, ItemView.this);cb_status = (CheckBox) view.findViewById(R.id.cb_status);tv_desc = (TextView) view.findViewById(R.id.tv_desc);tv_title = (TextView) view.findViewById(R.id.tv_title);}

要重复利用组合控件里面的三个子控件,就还要实现一下几个方法,分别是isChecked()判断CheckBox是否选中,setChecked(boolean checked)设置 CheckBox的状态,setDesc(String text)修改描述信息,setTitle(String text)修改标题等。

/** 组合控件是否有焦点* */public boolean isChecked(){return cb_status.isChecked();}/** 设置组合控件的状态* */public void setChecked(boolean checked){cb_status.setChecked(checked);}/** 组合控件更改文字* */public void setDesc(String text){tv_desc.setText(text);}/** 组合控件更改标题* */public void setTitle(String text){tv_title.setText(text);}

然后要使用这个组合控件的话就可以在activity_main.xml布局中直接调用自定义控件了。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><cn.edu.cqu.zhkj.ItemView android:id="@+id/iv_google"android:layout_width="match_parent"android:layout_height="wrap_content"/><cn.edu.cqu.zhkj.ItemView android:id="@+id/iv_gps"android:layout_width="match_parent"android:layout_height="wrap_content"/><cn.edu.cqu.zhkj.ItemView android:id="@+id/iv_es_gps"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>

在主布局文件中添加三个自定义的控件,然后在程序中进行设置。

package cn.edu.cqu.zhkj;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;public class MainActivity extends Activity{private ItemView iv_google;private ItemView iv_gps;private ItemView iv_es_gps;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv_google = (ItemView) findViewById(R.id.iv_google);iv_gps = (ItemView) findViewById(R.id.iv_gps);iv_es_gps = (ItemView) findViewById(R.id.iv_es_gps);iv_gps.setTitle("GPS卫星");iv_gps.setDesc("允许应用程序使用GPS对您进行定位");iv_es_gps.setTitle("使用增强型GPS");iv_es_gps.setDesc("可以使用服务器辅助GPS(取消选中可以降低网络使用率)");iv_google.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 判断是否选中if (iv_google.isChecked()) {iv_google.setChecked(false);}else {iv_google.setChecked(true);}}});iv_gps.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 判断是否选中if (iv_gps.isChecked()) {iv_gps.setChecked(false);}else {iv_gps.setChecked(true);}}});iv_es_gps.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 判断是否选中if (iv_es_gps.isChecked()) {iv_es_gps.setChecked(false);}else {iv_es_gps.setChecked(true);}}
});}}

这样就实现了组合控件的重复使用。

源码下载:组合控件

android 中的组合控件的设计相关推荐

  1. android中的标题栏是什么意思,Android通用标题栏组合控件

    原标题:Android通用标题栏组合控件 快,点击蓝色"字体"关注这个公众号,一起涨姿势 由于项目中经常用到此种组合控件,就封装了下,具体效果看下图,老司机可以绕道哈! 一.主要功 ...

  2. Android中的基础控件TextView、Button、ImageView、EditText、ProgressBar

    文章目录 1 Android中的基础控件 1.1 控件的通用属性 2 TextView 2.1 TextView的继承关系 2.2 TextView的常用属性 3 EditText 3.1 常用属性 ...

  3. Android中进度条控件使用

    android中进度条控件使用 ProgressBar pb = findViewById(R.id.pb);pb.setMax(100);pb.setProgress(33); 转载于:https: ...

  4. 从零开始学android:Android中的基本控件(上)

    从零开始学android:Android中的基本控件(上) 本章内容较多,下面只贴代码,大家只需要贴到自己eclipse里就知道作用^^! View组件简介 Android中的View组件包含了几乎所 ...

  5. Android Studio自定义组合控件

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

  6. android中的 listview控件,聊聊Android中的ListView控件

    软硬件环境 Macbook Pro MGX 72 Android Studio 1.3.2 坚果手机 前言 ListView是Android系统中使用非常广泛的一种控件,几乎所有的App都会用到它.它 ...

  7. android 中使用TabHost控件实现微信界面的底部菜单效果

    首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置) <TabHostandroid:id="@android:id/tabhost"android:layout ...

  8. Android中的基础控件CheckBox、RadioButton、ToggleButton、SeekBar

    文章目录 1 CheckBox 1.1 CheckBox介绍 2 RadioButton 2.1 RadioButton介绍 3 ToggleButton 3.1 ToggleButton介绍 4 S ...

  9. android中互斥的控件,Android控件之Radiobutton与RadioGroup

    RadioButton 是一个单选控件,在一个RadioGroup中,各个RadioButton是互斥的 XML文件: xmlns:tools="http://schemas.android ...

最新文章

  1. 邱键2008年最幸运的选手
  2. better-scroll:angularJs中用better-scroll封装一个滚动的指令
  3. “7th-place-solution-microsoft-malware-prediction”——kaggle微软恶意代码检测比赛第七名代码
  4. vulfocus靶场安装教程
  5. 边缘计算如何实现海量IoT数据就地处理
  6. JS脚本病毒调试脚本-Trojan[Downloader]:JS/Nemucod
  7. vs code ipynb文件_UE4引擎 源码的获取、安装,以及VS配置
  8. 坑爹的公交卡充值的流程
  9. Can't be easier
  10. super-smack
  11. Tolua 报错cannot load incompatible bytecode
  12. IDE安装与配置(2018)
  13. 计算机硬件故障的相关知识,电脑常见硬件故障大全
  14. 配置keepalived实现Nginx高可用(单主、双主模式)
  15. 个人作业——关于K米的产品案例分析
  16. 3D迷宫(DirextX9)
  17. pdf签名无效解决办法_强大的PDF编辑工具Wondershare PDFelement
  18. 【转】外连接&全连接的区别
  19. Linux服务器tomcat正常启动,但是通过IP不能访问web
  20. USBserver专业解决用友u8|金蝶等财务加密狗在虚拟机识别动态迁移解决方案

热门文章

  1. shared_ptr 循环引用问题以及解决办法
  2. 启明云端分享|大家都知道ESP32-C3是乐鑫5月刚量产的芯片,大家都传是取代ESP8266的,目前也确实有很多客户升级应用选择替换,那Esp8266与Esp32-C3 从硬件到应用开发有哪些不同呢
  3. php MySQL快速入门_PHP 连接 MySQL
  4. int类型存小数 mysql_MySQL面试题-数据类型
  5. linux 解析pdf下载工具,Linux高级系统级性能分析工具-perf.pdf
  6. android studio 实用指南,《Android Studio实用指南》4.27 使用演示模式(示例代码)
  7. vxworks操作系统_【7.10开播】最新自主研发工业操作系统发布会行业top来助阵,邀您共同见证(附报名)...
  8. 2018多校1 hdu6298 6300 6308
  9. 2019 湖南多校第五场题解
  10. iOS - app 进行安全加固