Android入门06 -- 自定义控件

自定义组合控件

  • 将几个子控件组合在一起,形成一个可复用的新的组合控件,自定义组合控件一般继承自RelativeLayout或者LinearLayout,其实现步骤如下:
    • 第一步:创建组合控件类继承自RelativeLayout或者LinearLayout
    • 第二步:创建layout布局文件,在组合控件的构造方法中,加载布局文件;
    • 第三步:为组合控件创建属性文件attrs.xml,在组合控件的构造方法中,加载属性文件,并给组合控件设置属性值;
    • 第四步:新的组合控件,供外界使用;
  • 现在我们来实现一个自定义组合控件,命名为SFCommonCell,实现的效果图如下所示:
image.png
  • 第一步:首先自定义组合控件类SFCommonCell,继承自RelativeLayout,代码如下:
package com.example.yyshop.general.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.yyshop.R;

public class SFCommonCell extends RelativeLayout {

    private ImageView iv_left;
    private TextView tv_text;
    private ImageView iv_right;

    private String text;
    private int textColor;
    private float textSize;
    private int leftIcon;
    private int rightIcon;

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

    public SFCommonCell(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SFCommonCell(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    private void initView(Context context,AttributeSet attrs) {
        //从XML中加载布局
        LayoutInflater.from(context).inflate(R.layout.sf_common_cell,this,true);
        iv_left = findViewById(R.id.sf_common_cell_iv_left);
        tv_text = findViewById(R.id.sf_common_cell_tv);
        iv_right = findViewById(R.id.sf_common_cell_iv_right);

        //从Attrs.xml中加载自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.SFCommonCell);
        text = typedArray.getString(R.styleable.SFCommonCell_text);
        textColor = typedArray.getColor(R.styleable.SFCommonCell_textColor, Color.BLACK);
        textSize = typedArray.getDimension(R.styleable.SFCommonCell_textSize,20f);
        leftIcon = typedArray.getResourceId(R.styleable.SFCommonCell_leftIcon,0);
        rightIcon = typedArray.getResourceId(R.styleable.SFCommonCell_rightIcon,0);

        //将Attrs.xml中属性值 赋值给目标控件
        setText(text);
        setTextColor(textColor);
        setTextSize(textSize);
        setLeftIcon(leftIcon);
        setRightIcon(rightIcon);

        //typedArray必须回收
        typedArray.recycle();
    }

    //设置属性
    public void setText(String text) {
        tv_text.setText(text);
    }

    public void setTextColor(int color) {
        tv_text.setTextColor(color);
    }

    public void setTextSize(float size) {
        tv_text.setTextSize(size);
    }

    public void setLeftIcon(int resourceId) {
        iv_left.setImageResource(resourceId);
    }

    public void setRightIcon(int resourceId) {
        iv_right.setImageResource(resourceId);
    }
}
  • 第二步:组合控件的布局文件为sf_common_cell.xml,实现如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="75dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/sf_common_cell_iv_left"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sf_common_cell_tv"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/sf_common_cell_iv_left"
        android:layout_marginLeft="15dp"/>

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/sf_common_cell_iv_right"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="15dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/grey"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>
  • 在组合控件的构造方法中,通过LayoutInflater去加载布局文件,实现如下:
image.png
  • 第三步:为组合控件提供自定义属性,可通过创建属性文件的方式,现创建属性文件attrs.xml,代码实现如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- SFCommonCell的自定义属性 -->
    <declare-styleable name="SFCommonCell">
        <!-- 标题 -->
        <attr name="text" format="string"/>
        <!-- 标题颜色 -->
        <attr name="textColor" format="color"/>
        <!-- 标题文字大小 -->
        <attr name="textSize" format="dimension"/>
        <!-- 左侧图片 -->
        <attr name="leftIcon" format="reference"/>
        <!-- 右侧图片 -->
        <attr name="rightIcon" format="reference"/>
    </declare-styleable>
</resources>
  • 在组合控件的构造方法中,加载属性文件,并将属性赋值给目标子控件,实现如下:
image.png
  • 注意⚠️:获取的属性typedArray,必须调用recycle进行回收,防止内存泄漏;
  • 第四步:外界使用自定义组合控件SFCommonCell,如下所示:
image.png
  • 在创建属性文件attrs.xml时,内部定义属性时,用到的format其类型有如下几种:
image.png
  • reference:参考某一资源ID,用于图片资源;
<attr name="leftIcon" format="reference"/>
app:leftIcon="@drawable/add_select"
  • color:颜色值;
<attr name="textColor" format="color"/>
app:textColor="@color/black"
  • string:字符串值;
<attr name="text" format="string"/>
app:text="中国工商银行"
  • dimension:尺寸值;
<attr name="textSize" format="dimension"/>
app:textSize="8sp"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 230,527评论 6 544
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,687评论 3 429
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 178,640评论 0 383
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,957评论 1 318
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,682评论 6 413
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 56,011评论 1 329
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 44,009评论 3 449
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 43,183评论 0 290
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,714评论 1 336
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,435评论 3 359
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,665评论 1 374
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 39,148评论 5 365
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,838评论 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 35,251评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,588评论 1 295
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,379评论 3 400
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,627评论 2 380

推荐阅读更多精彩内容