简体   繁体   English

图像按钮使应用程序崩溃

[英]Image Button crashes the app

When I add ImageButton s to my app (they don't have background), the app crashes. 当我将ImageButton添加到我的应用程序(它们没有背景)时,应用程序崩溃。 I have following code: 我有以下代码:

<ImageButton
    android:id="@+id/ib_main_home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/home"
    android:background="@null"
    android:scaleType="fitCenter"
    />

<ImageButton
    android:id="@+id/ib_main_categories"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/categories"
    android:background="@null"
    android:scaleType="fitCenter"
    />

<ImageButton
    android:id="@+id/ib_main_actors"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/actors"
    android:background="@null"
    android:scaleType="fitCenter"
    />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/twitter"
    android:background="@null"
    android:scaleType="fitCenter"
    />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/facebook"
    android:background="@null"
    android:scaleType="fitCenter"
    />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/instagram"
    android:background="@null"
    android:scaleType="fitCenter"
    />

(Whole xml is in RelativeLayout )... Without @null, image shows bigger than the screen. (整个xml在RelativeLayout中 )...如果没有@null,则图像显示的尺寸大于屏幕尺寸。 And Button fills the whole screen. 然后Button会填满整个屏幕。 Anyone can help me? 有人可以帮助我吗?

According to your comment that the error is java.lang.OutOfMemoryError, I suspect one or more of the images you are trying to load to the background take too much memory becuase they are too big . 根据您的评论,该错误是java.lang.OutOfMemoryError,我怀疑您尝试加载到后台的一个或多个图像占用了太多内存,因为它们太大了 The easiest solution will be to resize the images to a smaller size (and by this also lower your app's size). 最简单的解决方案是将图像调整为较小的尺寸(这样也可以减小应用程序的尺寸)。 You can resize manually or use one of the solutions offered online such as : http://resizeimage.net/ 您可以手动调整大小,也可以使用在线提供的解决方案之一,例如: http : //resizeimage.net/

For better result remove wrap_content and scale the images your self. 为了获得更好的效果,请删除wrap_content并缩放自己的图像。 You can shrink size of your source image.If you removed wrap_content also remove background="@null" this may help you. 您可以缩小源图像的大小。如果删除了wrap_content还可以删除background="@null"这可能会对您有所帮助。 Please tell me how do you want to organize these ImageViews. 请告诉我您如何组织这些ImageView。 You can user 'merge' tag for creating compound views , also see this link . 您可以使用'merge'标签来创建复合视图,也请参见此链接

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

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