可自由配置的图文混排控件——组合法
来源:互联网 发布:手机光照度测试软件 编辑:程序博客网 时间:2024/05/16 18:24
1、我们希望控件可以这样定制:
<com.itemp.imagetext.ImageText
android:layout_width="wrap_content" android:layout_height="wrap_content" talent:image_src="@mipmap/weather" talent:image_width="50dp" talent:image_height="50dp" talent:image_position="left" talent:text_size="15sp" talent:text_color="#f00" />
PS:当然,不要忘记自定义命名空间的引入
xmlns:talent="http://schemas.android.com/apk/res-auto"
2、为了实现上述美好设想,我们引入自定义的属性,位于res/values/attrs.xml中(如果你喜欢也可以叫fuck.xml)
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="ImageText"> <attr name="image_src" format="reference"/> <attr name="image_width" format="dimension"/> <attr name="image_height" format="dimension"/> <attr name="text_size" format="dimension"/> <attr name="text_color" format="color"/><!--枚举型自定义属性-->
<attr name="image_position" format="enum"> <enum name="left" value="0"/> <enum name="right" value="1"/> <enum name="top" value="2"/> <enum name="bottom" value="3"/> </attr> </declare-styleable></resources>3、令所有构造方法都去间接调用三个参数的构造方法public ImageText(Context context) { this(context,null);}public ImageText(Context context, AttributeSet attrs) { this(context, attrs,0);}4、在三个参数的构造方法中完成对自定义属性的读取public ImageText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //从界面中拿到属性的值 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageText);// float imageWidth = typedArray.getDimension(R.styleable.ImageText_image_width, 50); //TypedValue.COMPLEX_UNIT_DIP=dimension的单位,50=dimension的默认值 int imageWidth = (int) typedArray.getDimension(R.styleable.ImageText_image_width, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics())); int imageHeight = (int) typedArray.getDimension(R.styleable.ImageText_image_height, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics())); Drawable drawable = typedArray.getDrawable(R.styleable.ImageText_image_src); int imagePosition = typedArray.getInt(R.styleable.ImageText_image_position, 0); float textSize = typedArray.getDimension(R.styleable.ImageText_text_size, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics())); int textColor = typedArray.getColor(R.styleable.ImageText_text_color, Color.BLACK); typedArray.recycle(); Log.e(TAG, "ImageText:iw/dr/ip/tc="+imageWidth+"/"+drawable+"/"+imagePosition+"/"+textColor);//接下来根据不同的用户配置引入不同的布局 }5、根据不同的用户配置引入不同的布局(在布局中实现控件的“组合”)
if(imagePosition==0){ //将布局文件的丢到实例本身的肚子里(将“刘德华的样子”装入“自己的空皮囊”),将来贴到界面上给用户看 //最后一个参数,true=加载出来的View连同root一起返回给用户看 view = LayoutInflater.from(context).inflate(R.layout.widget_imagetext_left, this, true);}else { view = LayoutInflater.from(context).inflate(R.layout.widget_imagetext_right, this, true);}//找到子控件iv = ((ImageView) view.findViewById(R.id.iv));tv = ((TextView) view.findViewById(R.id.tv));//为控件设置值iv.setLayoutParams(new LayoutParams(imageWidth,imageHeight));iv.setImageDrawable(drawable);tv.setTextSize(textSize);tv.setTextColor(textColor);
OVER!
0 0
- 可自由配置的图文混排控件——组合法
- 一个简单的图文混排控件
- 打造原生的图文混排控件
- c++图文混排控件
- 自定义格式的图文混排编辑控件
- 图文混排 ——SpannableStringBuilder的使用
- 图文混排控件FreeTextBox过滤图片
- Android 自定义控件进阶:图文混排
- html——图文混排
- NGUI的图文混排
- UIButton的图文混排
- 图文混排的数据处理
- UIButton 的图文混排
- Label 的图文混排
- iOS 图文混排,UITableView实现图文混排 —— HERO博客
- 使用textview显示html里面的图文混排效果,并且图片可点击索引到
- Android 自定义可编辑图文混排EditText
- Android 自定义可编辑图文混排EditText
- 如何部署php 项目到 Wampserver
- 第十一天2017/04/25(2、二叉树)
- 分布式框架DSF的搭建
- Xcode8如何添加pch文件
- ViewPager 和 TabLayout
- 可自由配置的图文混排控件——组合法
- 2-线程的调度
- 魅族应用上传应用市场空包签名的问题,快捷解决方案!
- 视图
- 基于深度相机的三维重建技术
- Linux下安装MySQL-5.7
- 整理的最完整的OFPT_STATS_REQUEST && REPLY消息-部分转载(含有1.0与1.3的对比)
- flume使用
- C语言交换两个变量的值