自定义组合控件

自定义组合控件在安卓开发能起到不小的作用,当有几个控件需要重复的使用并且它们的布局相对来说也不会进行变化的话,这个时候就可以把它们封装成自定义组合控件,以实现后面的重用。

使用方法:

1、定义一个自定义组合控件的布局文件:

<?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="60dp" >
    <TextView android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textSize="20dp"
        android:text="这是自定义组合控件吗"/>
    <TextView android:id="@+id/tv_isCustom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title"
        android:textSize="15dp"
        android:text="这是自定义组合控件"
        android:layout_alignParentLeft="true"
        android:textColor="#77000000"/>
    <CheckBox android:id="@+id/cb"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:clickable="false"
        />
</RelativeLayout>

2、定义一个自定义组合控件类继承RelativeLayout,父类必须是viewgroup的子类,只有viewgroup才能容纳view,组合控件中必定是许多个控件组合在一起的,所以必须继承viewgroup的子类。

创建类后需要实现它的三个构造方法

//这个方法在代码中用于new一个控件对象时调用

public CustomView(Context context) {
  super(context);
   }

//这个方法在xml文件中添加控件时并需要样式的时候调用

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

//这个方法在xml文件中添加控件时调用

 public CustomView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

3、写好了三个构造方法接下来就是将类和xml布局文件进行关联,并且写控件的内部逻辑了

View.inflate(context, R.layout.customcontrol, this);//起到了上述的关联的作用

内部逻辑就不赘述了,这视情况而定的,可以参照代码看一看

自定义组合控件类:

package com.zyq.demo;

import com.example.demo.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 CustomView extends RelativeLayout {

 private TextView tv_title;
 private TextView tv_isCustom;
 private CheckBox cb;

 public CustomView(Context context) {
  super(context);
  iniView(context);
  // TODO Auto-generated constructor stub
 }

 public CustomView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  iniView(context);
 }

 public CustomView(Context context, AttributeSet attrs) {
  super(context, attrs);
  iniView(context);
  // TODO Auto-generated constructor stub
 }

 private void iniView(Context context) {
  // TODO Auto-generated method stub
  //把自定义组合控件的xml文件填充进这个类
  View.inflate(context, R.layout.customcontrol, this);
  tv_title = (TextView) this.findViewById(R.id.tv_title);
  tv_isCustom = (TextView) this.findViewById(R.id.tv_isCustom);
  cb = (CheckBox) this.findViewById(R.id.cb);
 }
 public boolean isChecked(){
  return cb.isChecked();
 }
 public void setChecked(boolean ischeck){
  cb.setChecked(ischeck);
  if(ischeck){
   tv_isCustom.setText("这是自定义组合控件");
  }else{
   tv_isCustom.setText("这不是自定义组合控件");
   
  }
 } 

}

调用类

<?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" >

    <com.zyq.demo.CustomView
        android:id="@+id/customview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </com.zyq.demo.CustomView>

</LinearLayout>

 

package com.zyq.demo;

import com.example.demo.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity implements OnClickListener {

 private CustomView custom;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  custom = (CustomView) this.findViewById(R.id.customview);
  custom.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if(custom.isChecked()){
   custom.setChecked(false);
  }else{
   custom.setChecked(true);
  }
 }
}


自定义组合控件属性的添加:

我们在安卓中所运用每一个控件都有自己的属性,例如TextView控件就能设置text用于显示文本,textsize控制字体大小等,这样使用控件很方便。

自定义组合控件也可以实现这样的效果。  以上面例子为基础进行修改。

组合控件的布局文件不用改变。

在/res/values目录下新建一个attrs.xml文件

declare-styleable标签下就是我们定义的属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="com.zyq.demo.CustomView">
        <attr name="title" format="string"></attr>
        <attr name="des_is" format="string"></attr>
        <attr name="des_no" format="string"></attr>
    </declare-styleable>
</resources>
这时候就可以在xml文件进行类似于安卓自带控件属性的设置

前提:必须使用自己工程所在资源包下的命名空间,属性前面用的是自定义的命名空间

例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:zyq="http://schemas.android.com/apk/res/com.example.demo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.zyq.demo.CustomView
        android:id="@+id/customview"
        zyq:title="是否是自定义组合控件"
        zyq:des_is="这是自定义组合控件"
        zyq:des_no="这不是自定义组合控件"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </com.zyq.demo.CustomView>

</LinearLayout>

 

然而还并没有写完,这样虽然不会报错,但并没有什么意义,属性的设置并不会体现到控件上,因为内部逻辑还没有写

回到定义控件的类,就我之前说到的在xml布局文件中调用的构造函数下,获取该控件的属性,然后设置组合控件中的子控件

代码:

package com.zyq.demo;

import com.example.demo.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 CustomView extends RelativeLayout {

 private TextView tv_title;
 private TextView tv_isCustom;
 private CheckBox cb;

 public CustomView(Context context) {
  super(context);
  iniView(context);
  // TODO Auto-generated constructor stub
 }

 public CustomView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  iniView(context);
 }

 public CustomView(Context context, AttributeSet attrs) {
  super(context, attrs);
  iniView(context);
  String title=attrs.getAttributeValue("
http://schemas.android.com/apk/res/com.example.demo","title");
  String des_is=attrs.getAttributeValue("
http://schemas.android.com/apk/res/com.example.demo","des_is");
  String des_no=attrs.getAttributeValue("
http://schemas.android.com/apk/res/com.example.demo", "des_no");
  tv_title.setText(title);
  if(isChecked()){
   tv_isCustom.setText(des_is);
   
  }else {
   tv_isCustom.setText(des_no);
  }
 }

 private void iniView(Context context) {
  // TODO Auto-generated method stub
  //把自定义组合控件的xml文件填充进这个类
  View.inflate(context, R.layout.customcontrol, this);
  tv_title = (TextView) this.findViewById(R.id.tv_title);
  tv_isCustom = (TextView) this.findViewById(R.id.tv_isCustom);
  cb = (CheckBox) this.findViewById(R.id.cb);
 }
 public boolean isChecked(){
  return cb.isChecked();
 }
 public void setChecked(boolean ischeck){
  cb.setChecked(ischeck);
  if(ischeck){
   tv_isCustom.setText("这是自定义组合控件");
  }else{
   tv_isCustom.setText("这不是自定义组合控件");
   
  }
 } 
}
这样就能完美使用自定义组合控件

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值