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
    评论
### 回答1: Android Button按钮的属性设置包括以下几个方面: 1. 文本属性:可以设置按钮的文本内容、字体大小、字体颜色、字体样式等。 2. 背景属性:可以设置按钮的背景颜色、背景图片、边框样式等。 3. 大小属性:可以设置按钮的宽度、高度、内边距、外边距等。 4. 状态属性:可以设置按钮的不同状态下的样式,如按下状态、禁用状态等。 5. 其他属性:还可以设置按钮的点击事件、可见性、透明度等其他属性。 以上是Android Button按钮的属性设置的主要内容,通过设置这些属性可以实现不同样式的按钮效果。 ### 回答2: Android Button按钮)是一种常用的UI组件,它用于在应用程序中添加交互元素。我们可以通过属性设置来定义一个Button的外观和行为。以下是一些常见的Button属性设置: 1. 文本属性 我们可以使用text属性来设置Button上显示的文本内容。我们还可以使用textColor属性来设置文本的颜色。通过设置textSize属性,我们可以改变文本的大小。 2. 背景颜色和背景图案 我们可以使用backgroundColor属性来设置Button的背景颜色。我们还可以使用background属性来设置Button的背景图案。我们可以通过xml文件来定义Button的背景图案。例如,我们可以定义一个shape来设置Button的背景色和边框效果。 3. 边框属性 我们可以使用borderWidth属性来设置边框的宽度。我们还可以使用borderColor属性来设置边框的颜色。另外,我们可以用radius属性来设置Button的圆角半径。 4. 状态选择器 我们可以使用state_pressed和state_focused属性来定义Button在不同状态下的效果。例如,当用户按下Button时,我们可以改变Button的背景色或文本颜色。我们可以通过selector xml文件定义状态选择器。 5. 点击事件 当用户点击Button时,我们可以为Button添加点击事件。我们可以通过setOnClickListener方法来为Button添加点击事件。当Button被点击时,我们可以执行一些额外的行为。例如,我们可以跳转到另一个Activity或在应用程序中执行某些操作。 总之,Button属性的设置可以让我们定制化自己的UI组件,提高用户体验。通过上述属性设置,我们可以打造各种不同样式的Button,让应用程序更加美观和实用。 ### 回答3: Android Button按钮是指用于触发一个动作或打开一个新的界面的可交互控件,它可以是文本按钮或图像按钮。在开发过程中,设置Button按钮的属性是非常重要的一步,下面我们来详细了解一下Android Button按钮的属性设置。 1. android:id属性:Button按钮在布局文件中需要指定id属性,以便在后台代码中对其进行操作。 2. android:text属性:Button按钮的文本中可以加入字符串或HTML代码,以满足不同需求的视觉效果。 3. android:textSize属性:Button按钮中文本的大小可以通过textSize属性调整。 4. android:textColor属性:用于控制Button按钮文本的颜色。 5. android:background属性:设置Button按钮的背景色,可以是颜色值或者是图片的资源文件。 6. android:onClick属性:指定Button按钮触发的事件处理程序,通过使用onClick事件处理程序,可以在Button按钮被点击时执行特定的操作。 7. android:enabled属性:设置Button按钮是否可点击,当设置为false时,Button将不会触发任何事件。 8. android:padding属性:设置Button按钮四周空白区域的大小。 9. android:visibility属性:控制Button按钮是否可见,常用的值包括visible、invisible、和gone。 10. android:layout_width和android:layout_height属性:用于指定Button按钮的宽度和高度。可选值包括match_parent和wrap_content。 以上是常见的Button按钮属性设置,它们可以帮助我们在开发过程中快速搭建出适用于不同移动端设备的用户交互界面。在使用Button按钮过程中,需要注意不同属性的取值范围和其对应的效果,这样才能更好地开发Android应用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值