Android 中的图像按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3318629/
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 09:41:21  来源:igfitidea点击:

ImageButton in Android

androidimagebutton

提问by Nandagopal T

Can anybody tell me how to resize the imageButtonto fit the image exactly? This is the code that I tried, but the image is placed at the position that I am locating using android:scaleType, but I am not able to reduce the size of imageButton. Please help me out in rectifying this issue. The code that I tried is:

谁能告诉我如何调整大小imageButton以完全适合图像?这是我尝试的代码,但图像放置在我使用 定位的位置android:scaleType,但我无法减小imageButton. 请帮我解决这个问题。我试过的代码是:

<ImageButton>
android:id="@+id/Button01"
android:scaleType="fitXY" // i have tried all the values for this attr
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="@drawable/eye"> // this is the image(eye)
</ImageButton>

采纳答案by Olivier Henny

Kinda late, but im all for 'better late then never' I had the same question today, found it explained here: http://www.anddev.org/tutorial_buttons_with_niceley_stretched_background-t4369.html

有点晚了,但我都是为了“迟到永远不要更好”我今天遇到了同样的问题,在这里找到了解释:http: //www.anddev.org/tutorial_buttons_with_niceley_stretched_background-t4369.html

回答by John Feagans

android:background="@drawable/eye"

works automatically.

自动工作。

android:src="@drawable/eye"

was what I used with all the problems of resizing the image the the width and height of the button...

是我在调整图像大小、按钮的宽度和高度的所有问题中使用的...

回答by Jorgesys

you are setting the image with the property "src"

您正在使用属性“src”设置图像

android:src="@drawable/eye">

use "background" property instead "src" property:

使用“ background”属性代替“ src”属性:

android:background="@drawable/eye"

like:

喜欢:

<ImageButton
  android:id="@+id/Button01"
  android:scaleType="fitXY" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:cropToPadding="false"
  android:paddingLeft="10dp"
  android:background="@drawable/eye"> // this is the image(eye)
</ImageButton>

回答by infinitypanda

You're probably going to have to resize the button programmatically. You'll need to explicitly load the image in your onCreate() method, and resize the button there:

您可能必须以编程方式调整按钮的大小。您需要在 onCreate() 方法中显式加载图像,并在那里调整按钮的大小:

public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  ImageButton myButton = (ImageButton) findViewById(R.id.button);  
  Bitmap image = BitmapFactory.decodeResource(R.drawable.eye);  
  myButton.setBitmap(image);  
  myButton.setMinimumWidth(image.getWidth());  
  myButton.setMinimumHeight(image.getHeight());
  ...
}

It's not guaranteed to work, according to the specifications for setMinimumX (since the width and height are still dependent on the parent view), but it should work pretty well for almost every situation.

根据 setMinimumX 的规范(因为宽度和高度仍然取决于父视图),它不能保证工作,但它几乎适用于所有情况。

回答by diexsie

Try to use ScaleType centerInside.

尝试使用 ScaleType centerInside。

ScaleTypes are not properly rendered in Eclipse Layout designer, so test in your running app.

ScaleTypes 未在 Eclipse 布局设计器中正确呈现,因此请在您正在运行的应用程序中进行测试。

回答by Arul

Did you try to give the layout_width and layout_height like the following? Since you are setting with wrap_content, the image button expands to the size of source image's height and width.

您是否尝试像下面这样给出 layout_width 和 layout_height?由于您使用 wrap_content 进行设置,因此图像按钮会扩展到源图像的高度和宽度的大小。

<blink>    
  <ImageButton>
    android:id="@+id/Button01"
    android:scaleType="fitXY" 
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:cropToPadding="false"
    android:paddingLeft="10dp"
    android:src="@drawable/eye"> 
  </ImageButton>
</blink>

回答by HimalayanCoder

You don't have to use it using src attribute

您不必使用 src 属性来使用它

Wrong way (The image won't fit the button)

错误的方式(图像不适合按钮)

android:src="@drawable/myimage"

Right way is to use background atttribute

正确的方法是使用背景属性

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/skin" />

where skinis an xml

其中皮肤是一个xml

skin.xml

皮肤文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- <item android:drawable="@drawable/button_disabled" android:state_enabled="false"/> -->
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
    <!-- <item android:drawable="@drawable/button_focused" android:state_focused="true"/> -->
    <item android:drawable="@drawable/button_normal"/>

</selector>

using button_pressed.png and button_normal.png

使用 button_pressed.png 和 button_normal.png

This will also help you in creating your skinned button with 4 states of pressed , normal , disabled and focussed. Make sure to keep same sizes of all pngs

这也将帮助您创建具有按下、正常、禁用和聚焦 4 种状态的皮肤按钮。确保所有 png 的大小保持相同

回答by Kislingk

You can also set background is transparent. So the button looks like fit your icon.

您还可以将背景设置为透明。所以按钮看起来适合你的图标。

 <ImageButton
    android:id="@+id/Button01"
    android:scaleType="fitcenter" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:cropToPadding="false"
    android:paddingLeft="10dp"
    android:background="@android:color/transparent"
    android:src="@drawable/eye" />

回答by khcpietro

I think you already solved this problem, and as other answers suggested

我认为你已经解决了这个问题,正如其他答案所建议的那样

android:background="@drawable/eye"

is available. But I prefer

可用。但是我更喜欢

android:src="@drawable/eye"
android:background="00000000" // transparent

and it works well too.(of course former code will set image as a background and the other will set image as a image) But according to your selected answer, I guess you meant 9-patch.

它也运行良好。(当然,以前的代码会将图像设置为背景,而另一个将图像设置为图像)但是根据您选择的答案,我猜您的意思是 9-patch。