Android实现自定义图片+文字控件

1、新建image_text_button.xml文件

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/image_text_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:animateLayoutChanges="true"
        android:background="@drawable/home_selector"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/button_icon"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:animateLayoutChanges="true"
            android:background="@android:color/transparent"
            android:scaleType="fitCenter">
        </ImageButton>

        <TextView
            android:id="@+id/button_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:animateLayoutChanges="true"
            android:textColor="@color/white"
            android:scaleType="fitCenter">
        </TextView>
    </LinearLayout>
</merge>

2、新建ImageTextButton.java

public class ImageTextButton extends LinearLayout {
    private Context mContext;
    private ImageButton mIcon;
    private TextView mText;
    private int mIconResourceId;

    public ImageTextButton(Context context) {
        super(context);
    }

    public ImageTextButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View.inflate(context, R.layout.image_text_button, this);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CarImageTextButton);

        mIcon = findViewById(R.id.button_icon);
        mIcon.setScaleType(ImageView.ScaleType.CENTER);
        mIcon.setClickable(false);
        mIconResourceId = typedArray.getResourceId(R.styleable.CarImageTextButton_icon, 0);
        mIcon.setImageResource(mIconResourceId);

        mText = findViewById(R.id.button_text);
        mText.setClickable(false);
        String text = typedArray.getString(R.styleable.CarImageTextButton_text);
        mText.setText(text);
    }
}

3、在attrs.xml文件中新增属性

    <!-- Allow for custom attribs to be added to a image text button -->
    <declare-styleable name="CarImageTextButton">
        <!-- icon to be rendered (drawable) -->
        <attr name="icon" format="reference"/>
        <attr name="text" format="string"/>
    </declare-styleable>

4、运用,使用icon和text属性配置图片和文字

需要在根目录标签新增xmlns:demo="http://schemas.android.com/apk/res-auto"属性

 <com.ad.demo.view.ImageTextButton
     android:id="@+id/go_home"
     style="@style/ImageTextButton"
     demo:icon="@drawable/b_poi_10010_67_hl"
     demo:text="回家"
     android:paddingStart="@dimen/default_6" />
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sunxiaolin2016

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值