Android: Problems with adjusting view size in an ImageButton

有些话、适合烂在心里 提交于 2019-12-12 04:25:32

问题


I'm trying something that should be simple, an imageButton and some text that says something about it. My problem is, the view of the imageButton is bigger than the image itself. I'm certain that the actual image is not like this (I've tried with several images and the result is the same). By searching online, the solution I've found is to insert the attribute "adjustViewBounds". That seemed reasonable but it didn't work. Here I leave my code and an image of how the result looks.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center|top">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@android:color/transparent"
        android:scaleType="fitXY"
        android:scaleX="0.2"
        android:scaleY="0.2"
        android:src="@drawable/icon1"
        android:padding="0dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DEFAULT!"
        android:textSize="20sp"
        />

</LinearLayout>

image


回答1:


The problem is using scaleX and scaleY to adjust the size. The button changes it's visible size, but the rest of the layout acts as if it were the original full scale.

I'd suggest using a different method of defining the size, for example:

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:background="@android:color/transparent"
        android:scaleType="fitXY"
        android:src="@drawable/icon1"
        android:padding="0dp"/>


来源:https://stackoverflow.com/questions/43105518/android-problems-with-adjusting-view-size-in-an-imagebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!