eclipse Android - 图像按钮大小

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

Android - Image button size

androideclipseimagebuttonimageview

提问by Mia

My imagebutton code wont respond to "fill_parent" and "wrap_content". Instead, it just shows the middle of the image (because it is a pretty big image). I tried setting specific values for the image but it still didn't work! Can somebody help me?

我的图像按钮代码不会响应“fill_parent”和“wrap_content”。相反,它只显示图像的中间(因为它是一个非常大的图像)。我尝试为图像设置特定值,但仍然无效!有人可以帮助我吗?

My button.xml in the res/drawable folder:

我的 button.xml 在 res/drawable 文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">\
<item android:state_pressed="true" 
android:drawable="@drawable/largishbutton" /> <!-- pressed -->
<item android:state_focused="true" 
android:drawable="@drawable/largerbutton2" /> <!-- focused -->
<item android:drawable="@drawable/largerbutton" /> <!-- default -->
</selector>

My main.xml in the res/layout folder:

我在 res/layout 文件夹中的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 android:background="@android:color/white">
 <ImageButton
 android:src="@drawable/button"
 android:layout_height="fill_parent"
 android:layout_width="wrap_content"
  /> 
 </RelativeLayout>

I tried using a LinearLayout, but the app wouldnt run, and I tried replacing the imagebutton with an imageview, but then the button didn't work.

我尝试使用 LinearLayout,但应用程序无法运行,我尝试用 imageview 替换 imagebutton,但该按钮不起作用。

回答by CommonsWare

If your image is bigger than your button, you will need to use something like android:scaleTypeor setMaxHeight()to have it resize to fit.

如果你的图片比你的按钮大,你需要使用类似的东西android:scaleType或者setMaxHeight()调整它的大小以适应。

Also, I recommend that you have all images in your <selector>be the same size, at least until you get things working the way you wish.

此外,我建议您将所有图像<selector>的大小都相同,至少在您按照您希望的方式工作之前是这样。