自定义组件系列---第一篇
来源:互联网 发布:行知职高厨师学费 编辑:程序博客网 时间: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
- 自定义组件系列---第一篇
- 自定义控件系列<第一篇>--多维表头
- ORMLite系列---第一篇
- Android 技术专题Feature系列第一篇 四大组件之Activity
- Android 技术专题Feature系列第一篇 四大组件之Service
- Android 技术专题Feature系列第一篇 四大组件之BroadcastReceiver
- 钓鱼系列之第一篇
- 排序算法系列第一篇
- 网站项目系列 第一篇
- Android TextView系列--第一篇
- 第一篇博客,Android四大组件
- iOS组件化方案-总结第一篇
- OpenStack学习系列-----第一篇 OpenStack介绍
- Base64系列第一篇 Base64介绍
- OpenStack学习系列-----第一篇 OpenStack介绍
- Oracle数据库经验系列---第一篇
- SpringBoot入门系列:第一篇 Hello World
- SpringBoot入门系列:第一篇 Hello World
- 免费 vpn 注册
- 分享资源
- Spring:java.lang.NoSuchMethodException: <init>()的解决办法
- nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- Markdown常用基本语法汇总
- 自定义组件系列---第一篇
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
- 深入理解RunLoop
- Android Binder机制(3) 本地服务注册过程
- 字幕整理:Fei-Fei Li: How computer understand the world?
- 网络:对称加密
- 初步认识Highcharts
- android-pulltorefresh设置header的问题
- codeforces 618F