简体   繁体   English

android:如何在 imageview 的水平中心对齐图像?

[英]android: how to align image in the horizontal center of an imageview?

I've tried all scaletypes, but all of them result in the image to be at the left corner of the imageview.我已经尝试了所有的刻度类型,但所有这些都会导致图像位于 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" />
<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

Your ImageView has the attribute wrap_content .您的 ImageView 具有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 .如果屏幕上只有图像视图,请尝试使用match_parent而不是wrap_content If you have more then one view in the layout you have to center the imageview.如果布局中有多个视图,则必须将图像视图居中。

You can center the ImageView itself by using:您可以使用以下方法将 ImageView 本身居中:

android:layout_centerVertical="true" or android:layout_centerHorizontal="true" For vertical or horizontal. android:layout_centerVertical="true"android:layout_centerHorizontal="true"垂直或水平。 Or to center inside the parent:或者在父级内部居中:

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.但听起来您正试图将实际源图像置于图像视图本身的中心,我不确定这一点。

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

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>

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

Try:尝试:

android:layout_height="wrap_content"
android:scaleType="fitStart"

on the image in the RelativeLayoutRelativeLayout的图像上

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>

you can horizontal your image view in a linear layout using:您可以使用以下方法在线性布局中水平放置图像视图:

 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"

使用这个属性: android:layout_centerHorizo​​ntal="true"

This is code in xml of how to center an ImageView, I used "layout_centerHorizontal".这是如何将 ImageView 居中的 xml 代码,我使用了“layout_centerHorizo​​ntal”。

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_centerHorizontal="true"
    >
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img2"
    />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img1"
        />
</LinearLayout>

or this other example...或者这个其他例子...

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    >
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img2"
    />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img1"
        />
</LinearLayout>

Give width of image as match_parent and height as required, say 300 dp.将图像的宽度指定为 match_parent,根据需要指定高度,例如 300 dp。

<ImageView
    android:id = "@+id/imgXYZ"
    android:layout_width = "match_parent"
    android:layout_height = "300dp"
    android:src="@drawable/imageXYZ"
    />

You can use many things if you are using LinearLayout then you can use android:layout_gravity="center"如果您使用 LinearLayout,您可以使用很多东西,那么您可以使用android:layout_gravity="center"

and if you are using RelaytiveLayout then you can use centerHorizontal="true"如果您使用的是 RelaytiveLayout 那么您可以使用centerHorizontal="true"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM