【组合控件】android自定义控件之带文字的ImageView

本文介绍如何在Android中创建一个自定义控件,该控件结合了ImageView和TextView的功能,使得可以在一个组件中同时显示图片和文本,以优化布局效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android自带的ImageView控件是不能添加文字的,如果出现大量ImageView和TextView同时出现的布局,可以组合定义一个控件,将ImageView和TextView组合成一个控件

如下图所示:


public class ImageButtonWithText extends LinearLayout {
    public   ImageView imageView;
    public TextView   textView;
    public ImageButtonWithText(Context context,AttributeSet attrs) {
        super(context,attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageButtonWithText);
        /*
        * 在attrs.xml添加属性:
        *   <declare-styleable name="ImageButtonWithText">
             <attr name="picture" format="reference"/>
            </declare-styleable>
        * */
        int picture_id = a.getResourceId(R.styleable.ImageButtonWithText_picture,-1);
        /**
         * Recycle the TypedArray, to be re-used by a later caller. After calling
         * this function you must not ever touch the typed array again.
         */
        a.recycle();
        imageView = new ImageView(context, attrs);
        imageView.setPadding(10, 10, 10, 10);
        /**
         * Sets a drawable as the content of this ImageView.
         * This does Bitmap reading and decoding on the UI
         * thread, which can cause a latency hiccup.  If that's a concern,
         * consider using setImageDrawable(android.graphics.drawable.Drawable) or
         * setImageBitmap(android.graphics.Bitmap) instead.
         * 直接在UI线程读取和解码Bitmap,可能会存在潜在的性能问题
         * 可以考虑使用 setImageDrawable(android.graphics.drawable.Drawable)
         * 或者setImageBitmap(android.graphics.Bitmap) 代替
         */
        imageView.setImageResource(picture_id);
        textView =new TextView(context, attrs);
        /**
         * Sets the horizontal alignment of the text and the
         * vertical gravity that will be used when there is extra space
         * in the TextView beyond what is required for the text itself.
         */
        //水平居中
        textView.setGravity(android.view.Gravity.CENTER_HORIZONTAL);
        textView.setPadding(0, 0, 0, 0);
        setClickable(true);
        setFocusable(true);
        setOrientation(LinearLayout.VERTICAL);
        addView(imageView);
        addView(textView);
    }
    public void setText(int resId) {
        textView.setText(resId);
    }
    public void setText(CharSequence buttonText) {
        textView.setText(buttonText);
    }
    public void setTextColor(int color) {
        textView.setTextColor(color);
    }
布局文件中这么使用:

<com.uestcneon.chuji.changjianglife.share.ImageButtonWithText
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    style="@style/hisCardTable"
    custom:picture="@mipmap/his_card_company"
    android:id="@+id/imgbtn_company"
    android:text="公司(1)" >
</com.uestcneon.chuji.changjianglife.share.ImageButtonWithText>
图片将通过自定义的custom:picture传递给ImageView控件,文字将通过android:text传递给TextView

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值