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

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);

 实例 DEMO http://pan.baidu.com/s/1c14HTW8

大道至简,知易行难,知行合一,得到功成;大道至简,悟在天成。
原文地址:https://www.cnblogs.com/XingzhiDai/p/5523513.html