Android 自定义组合控件

前言

我们会用到一些小的自定义控件,例如一个图标和一个字符的组合。虽然我们可以使用drawableLeft属性能实现这一功能,但在实际中往往不能满足需要,例如我们希望图标和字符能实现不一样的点击事件。

1. 两种常用的方式

  • 布局文件view_group_one.xml,一个线性布局,包含ImageViewTextView
    <?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:gravity="center_vertical"
        android:orientation="horizontal" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:src="@drawable/ok" />
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />
    </LinearLayout>
    
    自定义控件GroupOneView继承LinearLayout,在构造函数中导入view_group_one.xml,通过findViewById(int)方法查找控件,并修改字符。
    public class GroupOneView extends LinearLayout {
    
        public GroupOneView(Context context) {
            super(context);
        }
    
        public GroupOneView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
    
            View.inflate(context, R.layout.view_group_one, this);
    
            TextView tv =  findViewById(R.id.text_view);
            tv.setText("This is group one.");
        }
    
    }
    
  • 自定义控件GroupTwoView继承LinearLayout,在方法onFinishInflate()中通过findViewById(int)方法查找控件,并修改字符。
    public class GroupTwoView extends LinearLayout {
    
        public GroupTwoView(Context context) {
            super(context);
        }
    
        public GroupTwoView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
    
            TextView tv = findViewById(R.id.text_view);
            tv.setText("This is group two.");
        }
    
    }
    
    布局文件view_group_two.xml
    <?xml version="1.0" encoding="utf-8"?>
    <com.blog.demo.custom.widget.GroupTwoView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:src="@drawable/ok" />
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />
    </com.blog.demo.custom.widget.GroupTwoView>
    

2. 引用控件

GroupOneView可直接使用,GroupTwoView使用include引用。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.blog.demo.custom.widget.GroupOneView
        android:layout_width="match_parent"
        android:layout_height="40dp" />
    <include
        layout="@layout/view_group_two"/>
</LinearLayout>

效果如下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值