Android 填充不适用于 ImageButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20545742/
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
Padding not working on ImageButton
提问by Zero
In an app I am working on, I have several ImageButtons. Each ImageButton has a background and content in the form of a drawable. Right now the drawable is at maximum size within the confines of the ImageButton, but I want it to scale down so I need to add some padding. The thing is that when I try to do that, it doesn't have any effect. My XML is as follows for each ImageButton:
在我正在开发的应用程序中,我有几个 ImageButton。每个 ImageButton 都有一个可绘制形式的背景和内容。现在可绘制对象在 ImageButton 范围内处于最大尺寸,但我希望它按比例缩小,因此我需要添加一些填充。问题是,当我尝试这样做时,它没有任何效果。对于每个 ImageButton,我的 XML 如下:
<ImageButton
android:id="@+id/button_zero"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dip"
android:src="@drawable/button_zero"
android:background="@drawable/button_background" />
Any ideas why the padding doesn't do anything?
任何想法为什么填充不做任何事情?
Full XML code:
完整的 XML 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#222222"
tools:context=".Main" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Row One -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/button_zero"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dip"
android:src="@drawable/button_zero"
android:background="@drawable/button_front" />
<ImageButton
android:id="@+id/button_one"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dip"
android:src="@drawable/button_one"
android:background="@drawable/button_front" />
<ImageButton
android:id="@+id/button_two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dip"
android:src="@drawable/button_two"
android:background="@drawable/button_front" />
<ImageButton
android:id="@+id/button_three"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dip"
android:src="@drawable/button_three"
android:background="@drawable/button_front" />
<ImageButton
android:id="@+id/button_four"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dip"
android:src="@drawable/button_four"
android:background="@drawable/button_front" />
</LinearLayout>
... same for other rows
</LinearLayout>
回答by Gooziec
You have to add to Your ImageButton definition
您必须添加到您的 ImageButton 定义
android:scaleType="fitCenter"
or other scaleType like fitXY, because by default image try to scale as much as possible and ignore padding
或其他类似 fitXY 的 scaleType,因为默认情况下图像尝试尽可能多地缩放并忽略填充
回答by nicolas asinovich
Better use ImageViewthan ImageButtonif you need set image smaller with padding
如果您需要将图像设置为更小,则最好使用ImageView 而不是ImageButtonpadding
回答by PJ_Finnegan
The padding only has effect on the android:src
attribute, not on the android:background
.
填充仅对android:src
属性有效,对android:background
.
Set the first to your button image and the latter to android:background="@android:color/transparent"
将第一个设置为您的按钮图像,将后者设置为 android:background="@android:color/transparent"