Android自定义组合控件

步骤

 1、创建自定义控件布局xml

 2、声明一个View类 继承第1步中最外层的布局的类,一般为相对布局,或者线性布局 或者其他的ViewGroup。

public class SettingTextView extends RelativeLayout {
 3、在自定义的View对象里面重写它的构造方法。在构造方法里面就把布局都初始化完毕。
 4、根据业务需求 添加一些api方法,扩展自定义的组合控件;
 5、可以在布局文件里面 可以自定义一些属性。

自定义属性的步骤:

1、在res目录下的values目录下创建attrs.xml的文件 声明你写的属性。

     
        
        
        

1、使用自定义属性时注意声明自定义属性的命名空间(最后为你项目的包名)。
          xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"
2、在布局文件中使用你自定义的属性。下面的


3、获取这些定义的属性。自定义View对象的构造方法里面 有一个带两个参数的构造方法
   布局文件里面定义的属性都放在 AttributeSet attrs
/**
	 * 系统默认加载布局文件时,创建这个控件对象时调用这个方法
	 * @param context
	 */
	public SettingTextView(Context context, AttributeSet attrs) {

super(context,attrs); //获取自定义属性

title = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "title");update_on = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_on");update_off = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_off");initView(context);}


实例代码 

自定义组合控件布局




    

    

    
    
    

自定义组合控件类

package com.itheima.mobilesafe.ui;

import com.itheima.mobilesafe.R;
import com.itheima.mobilesafe.R.id;
import com.itheima.mobilesafe.R.layout;

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 SettingTextView extends RelativeLayout {

	private TextView tv_title;
	private TextView tv_desc;
	private CheckBox cb_status;
	private View v_line;
	private String update_on;
	private String update_off;
	private String title;
	 
	/**
	 * 在布局文件中指定空间时的样式时,系统默认加载布局文件时,创建这个控件对象时调用这个方法
	 * @param context
	 */
	public SettingTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	/**
	 * 系统默认加载布局文件时,创建这个控件对象时调用这个方法
	 * @param context
	 */
	public SettingTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		title = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "title");
		update_on = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_on");
		update_off = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_off");
		initView(context);
	}

	/**
	 * 在代码中new这样的对象使用这个方法
	 * @param context
	 */
	public SettingTextView(Context context) {
		super(context);
		
		initView(context);
	}
	
	public void initView(Context context){
		
		View view = View.inflate(context, R.layout.setting_text_view, this);
		tv_title = (TextView) view.findViewById(R.id.tv_title);
		tv_title.setText(title);
		tv_desc = (TextView) view.findViewById(R.id.tv_desc);
		cb_status = (CheckBox) view.findViewById(R.id.cb_status);
		v_line = (View) view.findViewById(R.id.v_line);
		
		
	}
	
	
	public void setChecked(boolean checked){
		cb_status.setChecked(checked);
		if(checked){
			tv_desc.setText(update_on);
		}else{
			tv_desc.setText(update_off);
		}
	}
	
	public boolean isChecked(){
		return cb_status.isChecked();
	}
		

}

布局中使用自定义组合控件

代码中使用自定义组合控件

private SettingTextView stv_update;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		
		stv_update = (SettingTextView) findViewById(R.id.stv_update);
		//回显是否更新的状态
		stv_update.setChecked(true);
		//添加自定义控件的单击事件的监听器
		stv_update.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//如果点击当前控件,设置复选框选中的状态
				if(stv_update.isChecked()){
					stv_update.setChecked(false);
				}else{
					stv_update.setChecked(true);
				}
			}
		});
	}

自定义属性xml




    
        
        
        
    





你可能感兴趣的:(android学习笔记,Android火速入门)