Android Imageview宽度与父对象匹配,需要水平居中

Android Imageview宽度与父对象匹配,需要水平居中,android,android-layout,android-imageview,Android,Android Layout,Android Imageview,我有一个高度和宽度都匹配的Imageview。我需要图像位于水平中心而不改变宽度以包裹内容。您可以更改父布局 <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true"

我有一个高度和宽度都匹配的Imageview。我需要图像位于水平中心而不改变宽度以包裹内容。您可以更改父布局

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



  <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="matrix"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

您应该使用android:scaleType=“fitCenter”进行尝试。Rest取决于您的确切需求以及所显示图像的大小

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
编辑

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
如果要仅将ImageView中的图像居中到水平居中,请添加以下内容:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
android:layout\u centerHorizontal=“true”

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
您的高度应为
wrap\u content

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerInside"
    android:src="@drawable/ic_launcher" />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>


这应该符合您的需要,如果您需要更多信息,请查看:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
请尝试以下代码:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>


我只需要水平中心的图像,而不需要在家长的中心。安卓:layout\u width=“fill\u parent”安卓:layout\u height=“fill\u parent”试试这个。你会明白的,p.s.你试过链接了吗,解释得很清楚。我试过链接。填充父项和匹配父项相同。我也试过fitStart。图像只在水平中心不需要垂直中心。你能发布你的问题的截图吗?打开这个我需要的图像只在水平中心,而不是在家长的屏幕上center@SamP为什么需要图像的高度和宽度与父级匹配?只需将android:layout\u centerHorizontal=“true”属性添加到