Android Button按钮周围添加图片

转载自:https://www.cnblogs.com/alimjan/p/7102001.html

xml:

<?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>

 java:

package com.example.alimjan.hello_world;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;


public class class__2_3_4 extends AppCompatActivity implements View.OnClickListener {
    private Button btn_icon;
    private Drawable drawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.code_2_3_4);
        btn_icon = (Button) findViewById(R.id.btn_icon);
        Button btn_left = (Button) findViewById(R.id.btn_left);
        Button btn_top = (Button) findViewById(R.id.btn_top);
        Button btn_right = (Button) findViewById(R.id.btn_right);
        Button btn_bottom = (Button) findViewById(R.id.btn_bottom);
        btn_left.setOnClickListener(this);
        btn_top.setOnClickListener(this);
        btn_right.setOnClickListener(this);
        btn_bottom.setOnClickListener(this);
        drawable = getResources().getDrawable(R.mipmap.ic_launcher); 
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),   // 必须设置图片大小,否则不显示图片                                  
        drawable.getMinimumHeight());
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_left) {
            btn_icon.setCompoundDrawables(drawable, null, null, null);
        } else if (v.getId() == R.id.btn_top) {
            btn_icon.setCompoundDrawables(null, drawable, null, null);
        } else if (v.getId() == R.id.btn_right) {
            btn_icon.setCompoundDrawables(null, null, drawable, null);
        } else if (v.getId() == R.id.btn_bottom) {
            btn_icon.setCompoundDrawables(null, null, null, drawable);
        }
    }

    public static void startHome(Context mContext) {
        Intent intent = new Intent(mContext, class__2_3_4.class);
        mContext.startActivity(intent);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值