简体   繁体   English

Android-图片按钮大小

[英]Android - Image button size

My imagebutton code wont respond to "fill_parent" and "wrap_content". 我的imagebutton代码不会响应“ fill_parent”和“ 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? 有人可以帮我吗?

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: 我的main.xml在res / layout文件夹中:

<?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,但该应用程序无法运行,并且尝试将imagebutton替换为imageview,但是该按钮不起作用。

If your image is bigger than your button, you will need to use something like android:scaleType or setMaxHeight() to have it resize to fit. 如果您的图片大于按钮,则需要使用android:scaleTypesetMaxHeight()来调整其大小以适合其尺寸。

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>中的所有图像都具有相同的大小,至少要等到您按自己的方式工作为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM