android - Imageview 宽度 match_parent 需要水平居中

标签 android android-layout android-imageview

我有高度和宽度为 match_parent 的 Imageview。我需要水平居中的图像将宽度更改为 wrap_content。您可以更改父布局。

这是我的 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="match_parent"
   android:layout_height="match_parent"
   android:scaleType="fitCenter"
   android:src="@drawable/ic_launcher" />

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

试试下面的代码:

    <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 - Imageview 宽度 match_parent 需要水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27395259/

相关文章:

android - 如何避免android中相对布局中的重叠 View ?

android - setVisibility 在 android 中为 null

android - 在 android 中为两个 ImageView 设置动画

android - 如何从 float 结果中删除小数?

android - 分页库BoundaryCallback的onItemAtFrontLoaded()方法的作用是什么?

java - Android:如何将字符串“R.string.a Button”转换为资源Restring.a Button?

java - libGDX - 对不同的屏幕使用不同的 InputProcessors?

java - 安卓资源$NotFoundException

Android:获取实际屏幕尺寸

android - java.lang.IllegalStateException : The specified child already has a parent