android:如何在图像视图的水平中心对齐图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2587305/
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
android: how to align image in the horizontal center of an imageview?
提问by Yang
I've tried all scaletypes, but all of them result in the image to be at the left corner of the imageview.
我已经尝试了所有比例类型,但所有这些都会导致图像位于图像视图的左角。
<ImageView
android:id="@+id/image"
android:scaleType="centerInside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:background="#0000"
android:src="@drawable/icon1" />
采纳答案by Janusz
Your ImageView has the attribute wrap_content
. I would think that the Image is centered inside the imageview but the imageview itself is not centered in the parentview. If you have only the imageview on the screen try match_parent
instead of wrap_content
. If you have more then one view in the layout you have to center the imageview.
您的 ImageView 具有属性wrap_content
。我认为图像在图像视图中居中,但图像视图本身不在父视图中居中。如果屏幕上只有图像视图,请尝试match_parent
代替wrap_content
. 如果布局中有多个视图,则必须将图像视图居中。
回答by Neil Chan
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
回答by Matt Swanson
You can center the ImageView itself by using:
您可以使用以下方法将 ImageView 本身居中:
android:layout_centerVertical="true"
or android:layout_centerHorizontal="true"
For vertical or horizontal. Or to center inside the parent:
android:layout_centerVertical="true"
或android:layout_centerHorizontal="true"
垂直或水平。或者在父级内部居中:
android:layout_centerInParent="true"
android:layout_centerInParent="true"
But it sounds like you are trying to center the actual source image inside the imageview itself, which I am not sure on.
但听起来您正试图将实际源图像置于图像视图本身的中心,我不确定这一点。
回答by klaydze
This works for me when aligning image view in linearlayout.
在线性布局中对齐图像视图时,这对我有用。
<ImageView android:id="@android:id/empty"
android:src="@drawable/find_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="center"
/>
回答by Abbes Yassine
Try this code :
试试这个代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/imgBusiness"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/back_detail" />
</LinearLayout>
回答by Mark Tickner
Try:
尝试:
android:layout_height="wrap_content"
android:scaleType="fitStart"
on the image in the RelativeLayout
在图像中 RelativeLayout
回答by Kovács Ede
For me android:gravity="center"
did the trick in the parent layout element.
对我来说android:gravity="center"
,在父布局元素中做到了这一点。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/fullImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/fullImageView"
android:layout_gravity="center" />
</LinearLayout>
回答by cdavidyoung
Using "fill_parent" alone for the layout_width will do the trick:
对 layout_width 单独使用“fill_parent”就可以了:
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:background="#0000"
android:src="@drawable/icon1" />
回答by hasanga lakdinu
you can horizontal your image view in a linear layoutusing:
您可以使用以下方法在线性布局中水平放置图像视图:
android:layout_gravity="center"
it will center your image to the parent element, if you just want to center horizontally you can use:
它将您的图像居中到父元素,如果您只想水平居中,您可以使用:
android:layout_gravity="center_horizontal"
回答by Thamo Tham
Use this attribute: android:layout_centerHorizontal="true"
使用这个属性: android:layout_centerHorizontal="true"