自定义组件系列---第一篇

来源:互联网 发布:行知职高厨师学费 编辑:程序博客网 时间:2024/05/22 04:28

效果图:

这里写图片描述

代码:

import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.admaster.SurveyPrize.R;/** * Created by lincoln on 16/4/16. */public class NormalButton extends LinearLayout {    private ImageView iconLeftIv;    private TextView contentTv;    private Context mContext;    //图片宽高    private int ivWidth = 80;    private int ivHeight = 50;    private boolean ivIsshow = false;    private Drawable imgSrc;    //字体相关    private int tvSize = 20;    private int tvColor = R.color.white;    private int tvMarginLeft = 5;    public NormalButton(Context context) {        super(context);        initViews(context, null);    }    public NormalButton(Context context, AttributeSet attrs) {        super(context, attrs);        initViews(context, attrs);    }    public NormalButton(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initViews(context, attrs);    }    private void initViews(Context context, AttributeSet attrs) {        //准备工作        if (attrs != null) {            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.Btn_normal);            ivHeight = typedArray.getDimensionPixelSize(R.styleable.Btn_normal_img_height, ivHeight);            ivWidth = typedArray.getDimensionPixelSize(R.styleable.Btn_normal_img_width, ivWidth);            ivIsshow = typedArray.getBoolean(R.styleable.Btn_normal_img_show, false);            imgSrc = typedArray.getDrawable(R.styleable.Btn_normal_img_src);            tvMarginLeft = typedArray.getDimensionPixelSize(R.styleable.Btn_normal_tv_marginLeft, tvMarginLeft);            tvSize = typedArray.getDimensionPixelSize(R.styleable.Btn_normal_tv_size, tvSize);            tvColor = typedArray.getColor(R.styleable.Btn_normal_tv_color,0XFFFFFFFF);            typedArray.recycle();        }        this.mContext = context;        //生成组件        if (ivIsshow) {            iconLeftIv = new ImageView(mContext);            iconLeftIv.setLayoutParams(new LayoutParams(ivWidth, ivHeight));            iconLeftIv.setImageDrawable(imgSrc);            addView(iconLeftIv);        }        contentTv = new TextView(mContext);        contentTv.setText("确定");        contentTv.setTextSize(tvSize);        contentTv.setTextColor(tvColor);        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        if (ivIsshow) {            params.leftMargin = tvMarginLeft;        }        contentTv.setLayoutParams(params);        //组装        setGravity(Gravity.CENTER);        addView(contentTv);    }}

attrs属性:

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- Normal Btn-->    <declare-styleable name="Btn_normal">        <attr name="img_width" format="dimension" />        <attr name="img_height" format="dimension" />        <attr name="img_src" format="reference" />        <attr name="img_show" format="boolean"/>        <attr name="tv_size" format="dimension" />        <attr name="tv_color" format="color" />        <attr name="tv_marginLeft" format="dimension"/>    </declare-styleable></resources>

引用:

<com.xxx.common.view.NormalButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background=            "@drawable/store_grid_btn_ok_selector"            lincoln:img_width="50dp"            lincoln:img_height="50dp"            lincoln:img_show="true"            lincoln:img_src=            "@drawable/store_list_item_gold"            lincoln:tv_size="15sp"            lincoln:tv_color="@color/white" />

参考1

0 0
原创粉丝点击