android自定义组合控件

本文介绍如何创建一个自定义组合控件,包括定义属性、布局和实现Java代码。通过实例展示了如何在布局文件中使用该控件,并监听其点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先定义组合控件的自定义属性

文件名是attrs.xml,放到value中

<?xml version="1.0" encoding="utf-8"?>
<resources>


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


</resources>

combinationView这个是组件的类型名字(类名)。


自定义组件的布局

<?xml version="1.0" encoding="utf-8"?>
<!-- 这个是自定义的控件中一个view的布局 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:example="http://schemas.android.com/apk/res/com.example.compadableview"
    android:id="@+id/rl_show_address"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@android:color/holo_blue_light"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="1dp"
        android:text="这是标题"
        android:textColor="#000000"
        android:textSize="20sp" />


    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="1dp"
        android:text="这是描述内容"
        android:textColor="#99ff0000"
        android:textSize="14sp" />


    <CheckBox
        android:id="@+id/cb_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:focusable="false" />
    <!-- 最后加一条分割线 -->


    <View
        android:layout_width="match_parent"
        android:layout_height="0.2dip"
        android:layout_alignBottom="@+id/cb_status"
        android:layout_marginTop="7dip"
        android:layout_alignParentBottom="true"
        
        android:background="#000000" />


</RelativeLayout>

然后自定义组件的java代码

package com.example.compadableview;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class Combinationview extends RelativeLayout {
private TextView tv_title;
private TextView tv_desc;
private Checkable cb_status;

//命名空间,在引用这个组件的时候需要用到
private String namespace="http://schemas.android.com/apk/res/com.example.compadableview";


//标题
private String title="title";

//被选中的描述
private String desc_on="desc_on";
//未被选中的描述
private String desc_off="desc_off";


public Combinationview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initView(context, attrs);
}

public Combinationview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//将自定义的布局渲染成View,一般都是layoutinflate,这个View inflate看看。
initView(context, attrs);
}
private void initView(Context context,AttributeSet attrs){
        View view=View.inflate(context, R.layout.layout_combinationview, this);

tv_desc=(TextView) view.findViewById(R.id.tv_desc);
tv_title=(TextView) view.findViewById(R.id.tv_title);
cb_status=(Checkable) view.findViewById(R.id.cb_status);

//获取属性 ,下面方法中第二个参数必须要是attrs.xml中自定义属性时指定的字符串。
title=attrs.getAttributeValue(namespace,title);
desc_on=attrs.getAttributeValue(namespace,desc_on);
desc_off=attrs.getAttributeValue(namespace,desc_off);

//初始化到子控件

if(title!=null){
tv_title.setText(title);
}
if(desc_off!=null){
tv_desc.setText(desc_off);
}
}
/**
* 判断是否被选中
* @return
*/
public boolean isChecked(){
return cb_status.isChecked();
}
/**
* 设置选中状态
* @param isChecked
*/
public void setChecked(boolean isChecked){
cb_status.setChecked(isChecked);
if(isChecked){
tv_desc.setText(desc_on);
}else{
tv_desc.setText(desc_off);
}
}
}

然后我们在布局中引用我们自定义的组件

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

    <com.example.compadableview.Combinationview
        
        android:id="@+id/cv_first"
        android:layout_width="wrap_content"
        example:desc_off="我是未被选中的描述1"
        example:desc_on="我是被选中的描述1"
        example:title="我是标题1"
        android:layout_height="wrap_content" >
    </com.example.compadableview.Combinationview>
    <com.example.compadableview.Combinationview
        android:id="@+id/cv_second"
        android:layout_width="wrap_content"
        example:desc_off="我是未被选中的描述2"
        
        example:desc_on="我是被选中的描述2"
        example:title="我是标题2"
        android:layout_height="wrap_content" >
    </com.example.compadableview.Combinationview>
    <com.example.compadableview.Combinationview
        android:id="@+id/cv_third"
        android:layout_width="wrap_content"
        example:desc_off="我是未被选中的描述3"
        example:desc_on="我是被选中的描述3"
        example:title="我是标题3"
        android:layout_height="wrap_content" >
    </com.example.compadableview.Combinationview>
    <com.example.compadableview.Combinationview
        android:id="@+id/cv_fourth"
        android:layout_width="wrap_content"
        example:desc_off="我是未被选中的描述4"
        example:desc_on="我是被选中的描述4"
        example:title="我是标题4"
        android:layout_height="wrap_content" >
    </com.example.compadableview.Combinationview>


</LinearLayout>


这里注意一下命名空间,要和自定义组件的包名一直,是清单文件中的包名



package com.example.compadableview;


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 Combinationview cv_first;
private Combinationview cv_second;
private Combinationview cv_third;
private Combinationview cv_fourth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
cv_first=(Combinationview) findViewById(R.id.cv_first);
cv_second=(Combinationview) findViewById(R.id.cv_second);
cv_third=(Combinationview) findViewById(R.id.cv_third);
cv_fourth=(Combinationview) findViewById(R.id.cv_fourth);

cv_first.setOnClickListener(this);
cv_second.setOnClickListener(this);
cv_third.setOnClickListener(this);
cv_fourth.setOnClickListener(this);

}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.cv_first:
if(cv_first.isChecked()){
cv_first.setChecked(false);
}else{
cv_first.setChecked(true);
}
break;
case R.id.cv_second:
if(cv_second.isChecked()){
cv_second.setChecked(false);
}else{
cv_second.setChecked(true);
}
break;
case R.id.cv_third:
if(cv_third.isChecked()){
cv_third.setChecked(false);
}else{
cv_third.setChecked(true);
}
break;
case R.id.cv_fourth:
if(cv_fourth.isChecked()){
cv_fourth.setChecked(false);
}else{
cv_fourth.setChecked(true);
}
break;


default:
break;
}
}





}

最后这步就是我么你怎么监听自定义控件的点击事件了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值