Android 将图片添加到按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12492064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:48:45  来源:igfitidea点击:

Android add image to button

androidimagebutton

提问by stealthjong

I want to add an image to a Button(no imagebutton, as the particular button can either contain text or an image; plus the imagebutton has the same problem), while still retaining the original android-style button. So adding android:backgroundin the XML doesn't cut it, as that will remove the android default button with rounded corners etc. (android.R.drawable.btn_default)

我想将图像添加到Button(不imagebutton,因为特定按钮可以包含文本或图像;加上图像按钮具有相同的问题),同时仍保留原始的 android 样式按钮。所以添加android:backgroundXML 并不会削减它,因为这将删除带有圆角等的 android 默认按钮。(android.R.drawable.btn_default)

Is this in any way possible?

这有可能吗?

I think one way is to make a 9patchimage for the button pressed (one for up and one for down) and onTouch Action_DOWNmake a layered background drawable and put that onto the button, and doing the same thing but with another 9patchfor onTouch Action_UP, but I think that will decrease the performance of the application substantially, as that will require quite a lot of resource reading and layer merging for all the button clicks (and for my application, that will be quite a lot). Is what I state above correct?

我认为一种方法是为9patch按下的按钮制作一个图像(一个用于向上,一个用于向下)onTouch Action_DOWN并使分层背景可绘制并将其放在按钮上,然后做同样的事情但使用另一个9patchfor onTouch Action_UP,但我认为这会显着降低应用程序的性能,因为这将需要大量资源读取和所有按钮点击的图层合并(对于我的应用程序,这将是相当多的)。我上面说的对吗?

EDIT: I can't declare the source of the image in the XML, because I get the images from a web service, so anything can be put on the Buttons, but it has to happen programmatically.

编辑:我无法在 XML 中声明图像的来源,因为我从 Web 服务获取图像,所以任何东西都可以放在按钮上,但它必须以编程方式发生。

回答by Ridcully

This can be done using the android:drawableTop, android:drawableBottom, android:drawableLeft, android:drawableRightattributes in your layout.xml.

这可以使用layout.xml 中的android:drawableTop, android:drawableBottom, android:drawableLeft,android:drawableRight属性来完成。

回答by Dhina k

enter image description here

在此处输入图片说明

     <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" />

for more info http://androiddhina.blogspot.in/2015/09/how-to-add-image-inside-button.html

更多信息http://androiddhina.blogspot.in/2015/09/how-to-add-image-inside-button.html

回答by Dipak Keshariya

Set below Property of Button for Display Image with Text, using this property image is displaying above text.

设置下面的按钮属性用于显示带有文本的图像,使用此属性图像显示在文本上方。

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

回答by Eldhose M Babu

For setting Image :

用于设置图像:

btn.setBackgroundResource(R.drawable.ic_launcher);

For setting text :

用于设置文本:

btn.setText("My Button");

Code :

代码 :

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");   
        }
    });
}

回答by weakwire

You can create your own drawable with multiple states and set that to background

您可以使用多种状态创建自己的可绘制对象并将其设置为背景

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

Drawables can have rounded corners like so.

Drawables 可以有这样的圆角。

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