安卓:如何给按钮添加图片

14
我想在一个按钮中添加图片(不是imagebutton),因为特定的按钮可以包含文本或图像,同时仍保留原始的Android样式按钮。因此,在XML中添加android:background无效,因为它会删除具有圆角等默认设置的Android按钮(android.R.drawable.btn_default)。这是否有可能?
我认为一种方法是为按下的按钮制作9patch图像(一个用于向上和一个用于向下),并在onTouch Action_DOWN时制作分层背景可绘制对象,并将其放置到按钮上,然后使用另一个9patch进行相同的操作以处理onTouch Action_UP,但我认为这会大大降低应用程序的性能,因为这将需要读取相当多的资源并合并所有按钮点击的图层(对我的应用程序来说,这将是相当多的)。我上面所说的正确吗?
编辑:我不能在XML中声明图像的来源,因为我从Web服务中获取图像,因此任何内容都可以放在按钮上,但必须通过编程方式实现。

你可以随时使用setBackgroundDrawable(R.drawable.my_selector)来设置背景图片。https://dev59.com/Nmcs5IYBdhLWcg3w5IBa#12492097 - weakwire
那么通过 Web 服务获取的图像怎么样? - stealthjong
5个回答

29
这可以通过在layout.xml中使用 android:drawableTop, android:drawableBottom, android:drawableLeft, android:drawableRight 属性来实现。

2
еңЁд»Јз ҒдёӯпјҢжӮЁеҸҜд»ҘдҪҝз”ЁеҘҮжҖӘе‘ҪеҗҚзҡ„ж–№жі•setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)пјҢеҰӮжӯӨжҸҸиҝ°пјҡhttp://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int) <-- й“ҫжҺҘжңӘиў« SO жӯЈзЎ®й“ҫжҺҘ... - Ridcully
刚刚得知您从网络服务获取图像,因此最好使用以下方法:setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)。您可以使用Drawable.createFromStream(InputStream is)创建可绘制对象。 - Ridcully
1
我该如何调整像这样使用的图像大小? - Francisco Peters

13

在此输入图片描述

     <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:background="@drawable/home_button"
            android:drawableLeft="@android:drawable/ic_menu_edit"
            android:drawablePadding="6dp"
            android:gravity="left|center"
            android:height="60dp"
            android:padding="6dp"
            android:text="AndroidDhina"
            android:textColor="#000"
            android:textStyle="bold" />

8

将下面的按钮属性设置为显示图像和文本,使用此属性可以在文本上方显示图像。

<Button android:drawableTop="@drawable/ic_launcher"/>

2

设置图片:

btn.setBackgroundResource(R.drawable.ic_launcher);

设置文本:

btn.setText("My Button");

代码:

private Drawable buttonDrawable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button btn=(Button) findViewById(R.id.button1);
    buttonDrawable=btn.getBackground();
    //Setting the image.
    btn.setBackgroundResource(R.drawable.ic_launcher);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            /*Removing image and setting text*/
            btn.setBackgroundDrawable(buttonDrawable);
            btn.setText("My Button");   
        }
    });
}

2
无法工作,因为我想保留 Android 风格的按钮。当我设置背景时,Android 原图标消失了,被一个可能是方形的按钮所取代。我希望图片在按钮内部而非替换原有的按钮布局。 - stealthjong

0
你可以创建自己的可绘制对象,具有多种不同的状态,并将其设置为背景。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" 
        android:drawable="@drawable/selected" />
    <item android:drawable="@drawable/normal" />
</selector>

可绘制对象可以具有圆角,如下所示。

<corners android:topLeftRadius="5dp" android:topRightRadius="5dp"
         android:bottomLeftRadius="0.2dp" android:bottomRightRadius="0.2dp"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接