可自由配置的图文混排控件——组合法

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!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的乔布梭

你好我是秦始皇转世,资助请从速

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值