Android layout - alignment issue with ImageView

2019-01-22 01:30发布

I'm trying to put an image at the top left of a LinearLayout, but with the image border and padding taking up the whole of the width of the window.

If I try the XML below, I get my image with its border and a white background across the whole width of the page, except that the image ends up centered, and doesn't move to the left.

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:src="@drawable/banner"
    android:background="@android:color/white"
    android:padding="@dimen/d_8px"
/>

Is there some attribute that I've not yet discovered that forces the image to be left aligned within the ImageView when the layout_width is set to fill_parent ?

In the mean-time I've worked around this by dropping the ImageView inside another LinearLayout, and dropping an empty TextView to its right that takes up the rest of the horizontal space.

2条回答
Viruses.
2楼-- · 2019-01-22 02:16

I was having a similar problem, to which you replied earlier today. Is this on "screen design" viewer provided by the Eclipse plugin side-by-side with the XML editor, or are you encountering the alignment problems when actually running the app? If the former, that appers to be a bug in the plugin, if the latter, try adding:

android:scaleType="fitStart"

From the documentation I've read, that seems to be the closest to what you need.

查看更多
疯言疯语
3楼-- · 2019-01-22 02:26

Just configure your ImageView on your layout XML file with something like this:

                            <ImageView android:id="@+id/imageViewName" 
                            android:layout_width="90dp" 
                            android:layout_height="90dp" 
                            android:scaleType="fitStart" 
                            android:adjustViewBounds="true" 
                            android:padding="10dp" 
                            android:src="@drawable/ic_contact_picture"
                            android:background="@color/white" />
查看更多
登录 后发表回答