Margin/Padding Shrinks ImageView

為{幸葍}努か 提交于 2019-12-10 11:27:17

问题


I recently tried to position an imageview's x and y coordinates with no luck, it seems there is no way to do it in Gingerbread. I then decided to try out paddings and margins, but when I set them, it shrinks my imageview. I set a left padding of 250dp and image view became tiny. The layout_height and width are set to wrap_content. I'm not sure what's going on. Does anyone know why setting a padding/margin would shrink an imageview?


回答1:


You're confusing margin and padding. Margin is the area outside of your view, while padding affects the content inside your margin.

If you set padding, then it is going to affect your available content area, and assuming you have a ScaleType set, it's going to shrink your image down to fit the available space.

Now, you say you've tried margins, but margins will do exactly what you're asking.

For example, if you wanted an ImageView placed 10dp from the top-left corner, you can do something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:id="@+id/my_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/my_image_id"
        />
</RelativeLayout>

Keep in mind that this places it 10dp with respect to the parent boundaries. If your parent layout also has padding, then that will affect your content placement.




回答2:


if by shrink you mean the picture's ratio is messed then you should use

android:scaleType="centerInside"

this will prevent the ratio from changing



来源:https://stackoverflow.com/questions/12169541/margin-padding-shrinks-imageview

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