繁体   English   中英

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

[英]android: how to align image in the horizontal center of an 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" />

您的 ImageView 具有wrap_content属性。 我认为图像在图像视图中居中,但图像视图本身不在父视图中居中。 如果屏幕上只有图像视图,请尝试使用match_parent而不是wrap_content 如果布局中有多个视图,则必须将图像视图居中。

您可以使用以下方法将 ImageView 本身居中:

android:layout_centerVertical="true"android:layout_centerHorizontal="true"垂直或水平。 或者在父级内部居中:

android:layout_centerInParent="true"

但听起来您正试图将实际源图像置于图像视图本身的中心,我不确定这一点。

在线性布局中对齐图像视图时,这对我有用。

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

试试这个代码:

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

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

尝试:

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

RelativeLayout的图像上

对我来说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>

您可以使用以下方法在线性布局中水平放置图像视图:

 android:layout_gravity="center"

它将您的图像居中到父元素,如果您只想水平居中,您可以使用:

 android:layout_gravity="center_horizontal"

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

这是如何将 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>

或者这个其他例子...

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

将图像的宽度指定为 match_parent,根据需要指定高度,例如 300 dp。

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

如果您使用 LinearLayout,您可以使用很多东西,那么您可以使用android:layout_gravity="center"

如果您使用的是 RelaytiveLayout 那么您可以使用centerHorizontal="true"

暂无
暂无

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

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