Android: ImageView in center screen

杀马特。学长 韩版系。学妹 提交于 2020-01-06 14:44:06

问题


I have a background image in full screen with a RelativeLayout. And other image (100x100) in an ImageView.

I want to put the ImageView in center screen. But my image shows top left.

What is the problem?

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fondo_inicio" >

<ImageView android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:background="@drawable/icono_inicio"/>

</RelativeLayout>

回答1:


try this to your ImageView

android:layout_centerInParent="true"



回答2:




Add the following to the ImageView.

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"



回答3:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fondo_inicio" >

<ImageView android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_centerInParent="true"
     android:background="@drawable/icono_inicio"/>

</RelativeLayout>



回答4:


<ImageView
        android:id="@+id/pic_id"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/pic" />



回答5:


Try this

android:layout_gravity="center"



回答6:


User layout_gravity and gravity doesn't work in case of RelativeLayout so just use

Layout_centerInParent = "true"


来源:https://stackoverflow.com/questions/23495060/android-imageview-in-center-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!