安卓开发学习之018 创建复合控件

本文介绍如何在Android应用中创建复合控件,包括定义布局、实现构造函数及使用方法。复合控件是一种自包含的视图组,由多个子视图组成,并可通过XML布局文件或代码构建。

复合控件即是指不可分割的、自包含的视图组,其中包含了多个排列和连接在一起的子视图。
当创建复合控件时,必须对它包含的视图的布局、外观和交互进行定义。复合控件是通过扩展一个ViewGroup(通常是一个布局)来创建的。以你,要创建一个新的复合控件,首先需要选择一个最合适放置子控件的布局类,然后扩展该类

public class MyCompoundView extends LinearLayout {
    public MyCompoundView(Context context) {
        super(context);
    }
    public MyCompoundView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

创建复合控件布局文件

与Activity一样,设计复合视图的UI布局的首选方法是使用外部资源
下面展示了一个简单复合控件的XML布局,该复合控件包含了一个EditText,EditText的下方是一个ClearText按钮,用来清除EditText中的内容
res/layout/clearable_edit_text.xml

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

    <EditText
        android:id="@+id/eidtText"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/clearButton"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:text="Clear"/>
</LinearLayout>

重写构造函数

要想在新的复合视图中使用此布局,需要对它的构造函数进行重写,使用LayoutInflater中的inflate方法来填充布局资源

public class MyCompoundView extends LinearLayout {
    private EditText mEditText;
    private Button mClearButton;

 //在XML中添加复合控件
    public MyCompoundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //使用布局资源填充视图
        LayoutInflater inflater = LayoutInflater.from(getContext());
        //还有两种写法
        //inflater= (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //inflater=((Activity)getContext()).getLayoutInflater();
        View view = inflater.inflate(R.layout.clearable_edit_text, this, true);

        //获得对子控件的引用
        mEditText = (EditText) view.findViewById(R.id.eidtText);
        mClearButton = (Button) view.findViewById(R.id.clearButton);
        //为按钮添加监听事件
        addClickLinstener();
    }
    private void addClickLinstener() {
        mClearButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditText.setText("");
            }
        });
    }
}

如果你更喜欢使用代码来构建自己的布局,那么就要重写第一个构造函数,如下

 //使用代码构建控件
    public MyCompoundView(Context context) {
        super(context);
        //将布局方向设置为纵向
        setOrientation(VERTICAL);
        //创建子控件
        mEditText = new EditText(context);
        mClearButton = new Button(context);
        mClearButton.setText("Clear");
        //在复合控件中布局他们
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams
                .MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mEditText.setLayoutParams(params);
        mClearButton.setLayoutParams(params);

        //将子控件添加到复合控件中
        addView(mEditText);
        addView(mClearButton);
        //为按钮添加监听事件
        addClickLinstener();

    }

使用复合控件的方法

在XML中使用自定义的复合控件

 <!-- 使用自定义复合控件-->
    <com.antex.mycompoundview.MyCompoundView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

使用代码生成自定义的复合控件

先得到父容器的引用,然后new一个复合控件,再添加到父容器中

        //父容器
        parent = (LinearLayout) findViewById(R.id.parent);
        //用java代码新建我们创建好的组合控件
        MyCompoundView compoundView = new MyCompoundView(this);
        //将控件添加到父容器中
        parent.addView(compoundView);

使用< include>标签引入复合控件的布局

 <!-- 使用布局创建复合控件-->
    <include android:id="@+id/clear_edit_text"
             layout="@layout/clearable_edit_text"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
        />

此种写法需要在Activity中添加子控件的点击监听事件

//引用使用布局创建的组合控件
        View view = findViewById(R.id.clear_edit_text);
        final EditText editText = (EditText) view.findViewById(R.id.eidtText);
        Button clearButton = (Button) view.findViewById(R.id.clearButton);
        clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });

开发工具:Android Studio1.5
SDK: Android 6.0
API 23

代码下载:018_MyCompoundView

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值