第十六天 自定义控件和自定义组合控件

1.  setContentView() 一旦调用,layout 会立即显示UI

2. inflate 只会将layout 形成一个以view类 实现 的对象 ,需要显示的时候还需要调用 setContentView() 。

----------------------------------------------------------------------------------------------

自定义控件组合

第一步 :先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中。item_config.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
     >

    <TextView
        android:id="@+id/tv_config_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="#000000"
        android:textSize="20sp" />

    <TextView
        android:layout_below="@+id/tv_config_title"
        android:id="@+id/tv_config_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="#99000000"
        android:textSize="16sp" />

    <CheckBox
           android:focusable="false"
           android:clickable="false"
        android:id="@+id/cb_config_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tv_config_title"
        android:layout_alignBottom="@+id/tv_config_description"
        android:layout_alignParentRight="true" />

    <View android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#22000000"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/tv_config_description"/>

</RelativeLayout>

2 。自定义Java类 加载布局文件,这个java类要继承ViewGroup ,并且实现3个构造方法

  ViewGroup ,可以将其他的布局文件,显示在自己的布局中。并根据需要 添加一些有用的api

package com.daiwang.mobilesafe.View;

import com.daiwang.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 ConfigSettingView extends RelativeLayout {
	private TextView tv_config_title;
	private TextView tv_config_description;
	private CheckBox cb_config_update;

	private String desc_on;
	private String desc_off;

	private void initViews(Context context) {
		//将一个RelativeLayout 布局文件,加载到ConfigSettingVieW 类中
		 //将一个布局文件转成一个view 对象。
		//root 这个布局文件加载到那个view 上面。
		View.inflate(context, R.layout.item_config, this);
		tv_config_title=(TextView) findViewById(R.id.tv_config_title);
		tv_config_description=(TextView) findViewById(R.id.tv_config_description);
		cb_config_update =(CheckBox) findViewById(R.id.cb_config_update);
	}

	private void SetConfigDesc(String string){
		tv_config_description.setText(string);
	}

	/**
     * 设置是否升级.
     */
	public void SetConfigUpdate(Boolean ischecked){
		cb_config_update.setChecked(ischecked);
		if(ischecked){
			SetConfigDesc(desc_on);
		}else{
			SetConfigDesc(desc_off);
		}

	}

	/**
	 * 检测选择状态
	 */
	public boolean isChecked(){
		return cb_config_update.isChecked();

	}

	public ConfigSettingView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		//构造函数初始化
		initViews(context);
	}

	//在布局文件加载是使用这个构造函数
	public ConfigSettingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initViews(context);
		//
		String title =attrs.getAttributeValue("http://schemas.android.com/apk/res/com.daiwang.mobilesafe", "title");
		desc_on =attrs.getAttributeValue("http://schemas.android.com/apk/res/com.daiwang.mobilesafe", "desc_on");
		desc_off =attrs.getAttributeValue("http://schemas.android.com/apk/res/com.daiwang.mobilesafe", "desc_off");

		tv_config_title.setText(title);
		tv_config_description.setText(desc_off);

	}

	public ConfigSettingView(Context context) {
		super(context);
		initViews(context);
	}

}

3 。在主布局文件中使用自定义的控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:daiwang="http://schemas.android.com/apk/res/com.daiwang.mobilesafe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/head"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="#8866ff00"
        android:gravity="center"
        android:text="设置选项"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000"
        android:textSize="22sp" />

    <com.daiwang.mobilesafe.View.ConfigSettingView
        android:layout_below="@+id/head"
        daiwang:title="升级选项"
     	daiwang:desc_on="升级选项打开"
     	daiwang:desc_off="升级选项关闭"
        android:id="@+id/zh_config_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.daiwang.mobilesafe.View.ConfigSettingView
        daiwang:title="杀毒选项"
     	daiwang:desc_on="杀毒选项打开"
     	daiwang:desc_off="杀毒选项关闭"
        android:id="@+id/zh_config_safe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/zh_config_update"
        android:layout_marginTop="10dp" />

    <com.daiwang.mobilesafe.View.ConfigSettingView
        daiwang:title="拦截选项"
     	daiwang:desc_on="拦截选项打开"
     	daiwang:desc_off="拦截选项关闭"
        android:id="@+id/zh_config_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/zh_config_safe"
        android:layout_marginTop="10dp" />

</RelativeLayout>

4 。为了自定义控件的更加方便,需要自定义命名空间

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:daiwang="http://schemas.android.com/apk/res/com.daiwang.mobilesafe"
              自定义写                                        包名

5. 自定义属性。在 /res/values/attrs.xml 文件中

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="ConfigSettingView">

        <attr name="title" format="string"  />

        <attr name="desc_on" format="string" />

        <attr name="desc_off" format="string" />
      </declare-styleable>

</resources>

6.使用自定义的属性

    <com.daiwang.mobilesafe.View.ConfigSettingView
        android:layout_below="@+id/head"
        daiwang:title="升级选项"
         daiwang:desc_on="升级选项打开"
         daiwang:desc_off="升级选项关闭"
        android:id="@+id/zh_config_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

7.布局文件的属性和对应的类进行关联。  在自定义类的构造函数中,通过类AttributeSet 取出布局文件中的属性。并且赋值

String title =attrs.getAttributeValue("http://schemas.android.com/apk/res/com.daiwang.mobilesafe", "title");
		desc_on =attrs.getAttributeValue("http://schemas.android.com/apk/res/com.daiwang.mobilesafe", "desc_on");
		desc_off =attrs.getAttributeValue("http://schemas.android.com/apk/res/com.daiwang.mobilesafe", "desc_off");

		tv_config_title.setText(title);
		tv_config_description.setText(desc_off);

实例 DEMOhttp://pan.baidu.com/s/1c14HTW8

时间: 2024-08-03 00:11:24

第十六天 自定义控件和自定义组合控件的相关文章

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

前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发成本,以及维护成本. 使用自定义组合控件的好处? 我们在项目开发中经常会遇见很多相似或者相同的布局,比如APP的标题栏,我们从三种方式实现标题栏来对比自定义组件带来的好处,毕竟好的东西还是以提高开发效率,降低开发成本为导向的. 1.)第一种方式:直接在每个xml布局中写相同的标题栏布局代码 <?xm

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

转自http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html 代码为自己编写 目标:实现textview和ImageButton组合,可以通过Xml设置自定义控件的属性. 通过代码或者通过xml设置自定义控件的属性 1.控件布局:以Linearlayout为根布局,一个TextView,一个ImageButton.  Xml代码 [html] view plaincopy < ?xml version="1.0" 

自定义组合控件和在自定义控件中使用自定义属性

今天,整理了一下我平时的笔记,写一个比较简单的自定义组合控件,仅供小白参考,大神请绕道,希望能够对大家有一些帮助 首先,得明白为什么我们需要自定义组合控件,它是因为原有控件并不能满足开发的需求,或者说并不能达到我们想要的一种效果,这个时候,就需要我们自己定义一些控件,以达到目的 ![先来看一下效果](http://img.blog.csdn.net/20160716224219109) 个人总结自定义控件的步骤: 1.先写一个布局,这里我用的是一个相对布局,我这里的相对布局就是根布局了 <?xm

Android自定义控件——自定义组合控件

转载请注明出处http://blog.csdn.net/allen315410/article/details/39581055  前面几篇博文介绍了Android如何自定义控件,其实就是讲一下如何"从无到有"的自定义一个全新的控件,继承View或者继承ViewGroup,复写其相关方法,这种自定义控件的方式相对来说难度较大,而且并不是所有需要新控件的情况下,都要这样进行.有很多情况下,我们只要运用好Android给我提供好的控件,经过布局巧妙的结合在一起,就是一个新的控件,我称之为&

Android实例-手机安全卫士(七)-自定义组合控件

一.目标. 将多个系统控件(TextView.Button.CheckBox等)组合成一个自定义的控件,并像系统控件一样使用.如图所示第1个自动更新控件是根据相对布局放置而成的,第2个自动更新控件即为自定义组合控件,它可以想一般的TextView等系统控件一样重复使用. 自定义控件如图: 二.代码实现. 1.在layout文件夹下新建一个xml文件(取名model_setting_item.xml),用于保存自定义控件的布局. 2.在新建的xml文件(model_setting_item.xml

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

自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需求,定义一些API方法: ----------------------------------4.根据需要,自定义控件的属性,可以参照TextView属性: 5.自定义命名空间,例如: xmlns:itheima="http://schemas.android.com/apk/res/<包名&

自定义组合控件,适配器原理-Day31

自定义组合控件,适配器原理-Day31 mobile2.1 主页定义 手机上锁功能 1.弹出设置密码框. 手机下载进度 自定定义控件 控件的属性其实就是控件类一个属性设置属性调用类的set方法方法, 自定义组合控件的思路 生命一个View对象继承自相对布局,线性布局或者其他的ViewGroup 在View对象重写构造方法,然后初始化布局,通过View.inflate()方法把我们自己定义的布局挂到界面当中. 自定义属性在res/values目录下创建attrs.xml里面定义一些属性, <res

android 自定义组合控件

自定义控件是一些android程序员感觉很难攻破的难点,起码对我来说是这样的,但是我们可以在网上找一些好的博客关于自定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲下自定义组合控件,这个特别适合在标题栏或者设置界面,看下面图: 就非常适合使用组合控件了,现在写一个玩玩: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android 自定义组合控件小结

引言 接触Android UI开发的这段时间以来,对自定义组合控件有了一定的了解,为此小结一下,本文小结内容主要讨论的是如何使用Android SDK提供的布局和控件组成一个功能完整组合控件并将其封装为面向对象的类,而并非讨论如何继承自SDK提供的控件类(比如TextView),对其进行自定义扩展的问题. 进入正题前,我们先来看一组功能需求 假设在手机需求上,那么如上三个界面我们可以使用三个Activity,每个Activity一个布局文件,实现起来比较独立,但是假设在Android pad上要