Android:layout_width 和 height 裁剪我的图像

标签 android layout imagebutton

我将这个小布局定义为插入到我的应用程序的每个页面中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:background="@null"
    android:src="@drawable/sa_info"
    android:layout_alignParentRight="true"/>

我的 sa_info.png 是 118x118,但我希望它在屏幕上看起来小得多。如果我尝试将 imagebutton 的宽度和高度设置为所需的大小(比如说 48dp),那么当我运行应用程序时生成的图像将被裁剪到中心: original image cropped image

所以我必须获得所需结果的唯一方法是将图像本身缩放到所需的分辨率。 我做错了什么,有更好的方法吗?

最佳答案

尝试添加

android:scaleType="fitCenter" -> http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

android:adjustViewBounds="true" -> 如果您希望 ImageView 调整其边界以保持其可绘制对象的纵横比,请将此设置为 true。

关于Android:layout_width 和 height 裁剪我的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19976612/

相关文章:

Android Studio 错误 : installed build tool revision 32. 0.0 已损坏

java - getIntent() 返回空指针异常

java - 尝试检索和裁剪图像后未返回任何数据

java - 使用 PDF 查看器库时出现空指针异常

css - 如何在固定宽度元素上设置全屏宽度背景?

Android:如何根据大小创建自定义布局

java - 如何在 Swing 中绘制布局单元格及其边缘?

java - ImageButton 设置LayoutParams 后图像模糊

android - 在 ImageButton 中动态更改图像

android - libGDX - 从图像按钮中删除颜色