Button文字周围放置图片

设置图形对象的矩形边界大小,注意必须设置图片大小,否则不会显示图片

  setbounds的4个参数,

  前两个int 是矩形组件左上角那个点在容器中的坐标。
  后两个int 是矩形组件的宽度和高度。

 

MainActivity

package com.example.junior;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;


public class IconActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_icon; // 声明一个按钮对象
    private Drawable drawable; // 声明一个图形对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_icon);
        // 从布局文件中获取名叫btn_icon的按钮控件
        btn_icon = findViewById(R.id.btn_icon);
        // 从资源文件ic_launcher.png中获取图形对象
        drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        // 设置图形对象的矩形边界大小,注意必须设置图片大小,否则不会显示图片
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        // 下面通过四个按钮,分别演示左、上、右、下四个方向的图标效果
        findViewById(R.id.btn_left).setOnClickListener(this);
        findViewById(R.id.btn_top).setOnClickListener(this);
        findViewById(R.id.btn_right).setOnClickListener(this);
        findViewById(R.id.btn_bottom).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {  // 一旦监听到点击动作,就触发监听器的onClick方法
        if (v.getId() == R.id.btn_left) {
            // 设置按钮控件btn_icon内部文字左边的图标
            btn_icon.setCompoundDrawables(drawable, null, null, null);
        } else if (v.getId() == R.id.btn_top) {
            // 设置按钮控件btn_icon内部文字上方的图标
            btn_icon.setCompoundDrawables(null, drawable, null, null);
        } else if (v.getId() == R.id.btn_right) {
            // 设置按钮控件btn_icon内部文字右边的图标
            btn_icon.setCompoundDrawables(null, null, drawable, null);
        } else if (v.getId() == R.id.btn_bottom) {
            // 设置按钮控件btn_icon内部文字下方的图标
            btn_icon.setCompoundDrawables(null, null, null, drawable);
        }
    }
}

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:drawableLeft="@mipmap/ic_launcher"
        android:drawablePadding="5dp"
        android:text="热烈欢迎"
        android:textColor="#000000"
        android:textSize="17sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_left"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="图标在左"
            android:textColor="#000000"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btn_top"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="图标在上"
            android:textColor="#000000"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btn_right"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="图标在右"
            android:textColor="#000000"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btn_bottom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="图标在下"
            android:textColor="#000000"
            android:textSize="15sp" />

    </LinearLayout>

</LinearLayout>

result

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值