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!