[英]How can I stretch the image in imagebutton to the whole area of the imagebutton?
这是一张可以帮助您理解我的问题的图片:
我想舒展内显示的图像ImageButton
到整个区域ImageButton
。 可以看到,编号为 1 的图片只占据了ImageButton
95% 左右,并且还可以看到ImageButton
的边界。
我还是想保持图片按钮样式
这是我如何使用 ImageView:
<ImageButton
android:id="@+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:src="@drawable/111" />
我想我发现了我的问题所在。 我需要一个只能包含图像并具有点击效果的自定义组件。
ImageButton 是 ImageView。
如果你想让你的图像填充按钮
比设置android:scaleType="fitXY"
,因此您在android:src
设置的图像将被拉伸以填充整个图像按钮。
ImageButton 在您的图像和填充下有一个背景。
如果你不想背景
设置android:background="@android:color/transparent"
。
如果你不想填充
设置android:padding="0"
或设置没有填充的背景。
你也可以使用
android:scaleType="centerCrop"
android:padding="0dp"
android:scaleType="fitXY"
测试并正常工作。
使用以下代码:-
<ImageButton
android:id="@+id/secondActivityBbuttonDownRight"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:scaleType="fitStart"
android:background="@drawable/111" />
您应该考虑创建一个 9-patch 图像以使图像正确地适合ImageButton
,以防您需要拉伸它,但又不希望图像失真。
要拥有一个带有完全覆盖它的图像的按钮,您可以使用以下内容:
<ImageButton android:id="@+id/iconBtn"
android:layout_width="64dip"
android:layout_height="64dip"
android:src="@drawable/icon_button"
android:scaleType="fitCenter"
android:background="#00000000"/>
有关创建具有自定义外观的按钮以及使用选择器管理其状态的完整教程,您可以查看我前段时间在这里编写的教程(Creating a custom Android button with a resizable skin) 。
如果您的图像“111”本身没有填充,那么只需使用 android:background"@drawable/111"
只需在ImageButton
上设置这两个属性:
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
有了这些,你将得到所提供的 src 被拉伸,而且如果你在ImageButton
上放置一些特定的width
和height
,你将不会在它周围得到一个矩形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.