Android 布局 - ImageView 对齐问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/655994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:22:43  来源:igfitidea点击:

Android layout - alignment issue with ImageView

androidlayoutandroid-imageview

提问by Alnitak

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.

我试图将图像放在 a 的左上角LinearLayout,但图像边框和填充占据了窗口的整个宽度。

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

如果我尝试下面的 XML,我会得到我的图像,它的边框和整个页面宽度的白色背景,除了图像最终居中,并且不会向左移动。

<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 ImageViewwhen the layout_widthis set to fill_parent?

是否有一些我尚未发现的属性会强制图像在设置为ImageView时左对齐?layout_widthfill_parent

In the meantime, I've worked around this by dropping the ImageViewinside another LinearLayoutand dropping an empty TextViewto its right that takes up the rest of the horizontal space.

与此同时,我解决了这个问题,把ImageView里面的另一个LinearLayout放下,TextView在它的右边放下一个空的,占据了其余的水平空间。

回答by executor21

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:

我遇到了类似的问题,你今天早些时候回复了。这是在 Eclipse 插件与 XML 编辑器并排提供的“屏幕设计”查看器上,还是在实际运行应用程序时遇到对齐问题?如果是前者,那似乎是插件中的一个错误,如果是后者,请尝试添加:

android:scaleType="fitStart"

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

从我读过的文档来看,这似乎最接近您的需要。

回答by Oscar Salguero

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

只需使用以下内容在布局 XML 文件上配置 ImageView:

                            <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" />