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