美文网首页
Android 自定义组合控件

Android 自定义组合控件

作者: 付凯强 | 来源:发表于2024-04-09 22:13 被阅读0次

所谓组合控件,指的是把系统现有的控件组合在一起形成一个新控件。这里我们自定义一个LinearLayout控件,LinearLayout控件中又含有RelativeLayout控件,RelativeLayout控件中有TextView控件和RadioGroup控件。布局如下:

combined_view.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="wrap_content"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv"
            android:layout_marginStart="10dp"
            android:layout_centerVertical="true"
            android:layout_alignParentStart="true"
            android:text="你的性别是?"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:id="@+id/rg"
            android:layout_centerVertical="true"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="10dp"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/bt_01"
                android:text="男"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton
                android:id="@+id/bt_02"
                android:text="女"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </RadioGroup>
    </RelativeLayout>

</LinearLayout>

控件的布局有了,需要把当前布局添加到控件树中。

CustomCombinedView

创建类CustomCombinedView,继承自LinearLayout,在构造方法中进行初始化

    public CustomCombinedView(Context context) {
        this(context,null);
    }

    public CustomCombinedView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomCombinedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.combined_view, this);
        TextView textView = view.findViewById(R.id.tv);
        RadioGroup radioGroup = view.findViewById(R.id.rg);
        textView.setText("不是默认的字符串");
        textView.setTextColor(Color.RED);
        radioGroup.setOnCheckedChangeListener(this);
    }

首先通过View.inflate将当前的布局添加到控件树中去,然后通过返回的View获取布局中的子控件。特意选了RadioGroup,这里可以通过对RadioGroup的监听加一些逻辑:RadioGroup setOnCheckedChangeListener:

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.bt_01) {
            Toast.makeText(getContext(),"I am man.",Toast.LENGTH_SHORT).show();
        } else if (checkedId == R.id.bt_02) {
            Toast.makeText(getContext(),"I am women.",Toast.LENGTH_SHORT).show();
        }
    }

点击不同的RadioButton会弹不同内容的Toast。

总结

自定义组合控件大致分为以下几步:

  1. 书写布局
  2. 将布局以代码的方式添加到控件树中,并得到对应的View对象
  3. 通过View对象可以获得子控件的Id,通过Id可以自定义处理逻辑。

相关文章

  • Android自定义控件之自定义组合控件

    Android自定义控件之自定义组合控件 前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原...

  • 【Android】自定义控件

    Android自定义控件可以有三种实现方式 组合原生控件自己绘制控件继承原生控件 1 组合原生控件 1.1 组合原...

  • Android中的自定义控件

    Android中的自定义控件大致可以分成三类:自定义组合控件、继承原生控件的自定义控件、继承View自己实现绘制的...

  • 自定义的控件简介

    android 自定义控件简介 安卓中的自定义控件可以分为三种: 通过将系统提供的控件组合,成为新的控件 自定义V...

  • 自定义控件的原因以及动画的分类

    什么是自定义控件? 在 Android 系统中使用系统自带控件重新组合或者自定义类继承 View / ViewGr...

  • 组合自定义控件的步骤详解

    Android 步骤: 1 自定义组合控件的布局settint_view.xml 2 创建一个自定义子和控件的类S...

  • 自定义控件概述

    阅读原文 1.自定义控件的一些概念 1.什么是自定义控件 在Android系统中,用系统的自带控件重新组合或者自定...

  • Android自定义控件(一,基本原理)

    自定义控件相关目录: Android自定义控件(一,基本原理) Android自定义控件(二,自定义属性) And...

  • Android入门06 -- 自定义控件

    自定义组合控件 将几个子控件组合在一起,形成一个可复用的新的组合控件,自定义组合控件一般继承自RelativeLa...

  • Android开发之自定义View(一)

    Android常见的自定义控件有三种方式: 继承View 继承原有的控件,在原有控件的基础上进行修改 重新拼装组合...

网友评论

      本文标题:Android 自定义组合控件

      本文链接:https://www.haomeiwen.com/subject/gvafxjtx.html