Android自定义控件扩展示例
来源:互联网 发布:免费开源电商系统源码 编辑:程序博客网 时间:2024/06/03 07:52
通过扩展布局的方式,创建一个图片和文字在一起的button
Android代码如下:
package com.example.demo.practice.ui;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.example.demo.practice.R;public class ImageIcon extends LinearLayout { public ImageIcon(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(VERTICAL); setGravity(Gravity.CENTER_HORIZONTAL); //获取属性 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageIcon); String title = typedArray.getString(R.styleable.ImageIcon_title_text); int text_color = typedArray.getInt(R.styleable.ImageIcon_text_color, 0); float text_size = typedArray.getDimension(R.styleable.ImageIcon_title_size, 0); Drawable icon = typedArray.getDrawable(R.styleable.ImageIcon_icon_pic); int icon_size = typedArray.getDimensionPixelSize(R.styleable.ImageIcon_icon_size,0); //实例化控件 ImageView imageView = new ImageView(context); TextView textView = new TextView(context); imageView.setImageDrawable(icon); textView.setText(title); textView.setTextColor(text_color); textView.setTextSize(text_size); //添加控件,设置大小 addView(imageView); addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams(); layoutParams.width=icon_size; layoutParams.height=icon_size; typedArray.recycle(); }}
自定义属性:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="ImageIcon"> <attr name="icon_pic" format="reference|color"/> <attr name="title_text" format="string"/> <attr name="title_size" format="dimension"/> <attr name="icon_size" format="dimension"/> <attr name="text_color" format="color"/> </declare-styleable></resources>
使用:
<com.example.demo.practice.ui.ImageIcon android:layout_width="wrap_content" android:layout_height="wrap_content" app:icon_pic="@drawable/icon_fault" app:title_text="@string/login" app:icon_size="60dp" app:title_size="5sp" app:text_color="@color/black"> </com.example.demo.practice.ui.ImageIcon>
结果图:
阅读全文
0 0
- Android自定义控件扩展示例
- android自定义倒计时控件示例
- [Android]自定义控件入门示例
- android自定义TabHost扩展控件TabView
- android自定义TabHost扩展控件TabView
- Android 自定义属性,系统控件扩展
- Android 自定义属性,系统控件扩展
- android自定义TabHost扩展控件TabView
- Android 自定义属性,系统控件扩展
- Android 自定义扩展控件 一:ClearEditText
- Android 自定义属性,系统控件扩展
- Android自定义控件---继承ProgressBar功能扩展
- Android 自定义属性,系统控件扩展
- Android 自定义属性,系统控件扩展
- android自定义控件以及复用控件示例详解
- WinForm自定义控件扩展
- 自定义扩展Repeater控件
- 自定义控件-WebControl示例
- 南阳SEO:网站SEO更新文章6原则,做好避免无用功
- ESP8266+钛极OS(TiJOS)+面包板=JAVA控制物联网硬件平台[创客版]
- ==和equals的区别
- 微信小程序-五星评分功能
- 跟我一起读源码(java)—String、StringBuffer、StringBuilder
- Android自定义控件扩展示例
- 从《深入分析Java Web开发技术内幕》学到的东西
- 直方图
- 安卓最常用的框架,快速开发
- 299. Bulls and Cows
- Hive编程(十一)【其他文件格式和压缩方法】
- bzoj 4756: [Usaco2017 Jan]Promotion Counting
- 股票大盘常用指标
- RF-常用关键字-log