Center ImageView inside another ImageView in Android

后端 未结 4 1733
无人共我
无人共我 2021-02-10 06:52

Well I have to fit one ImageView inside another one. It is smaller and has to be exactly at the center. I have both images scaled for different screen resolutions but I can test

相关标签:
4条回答
  • 2021-02-10 07:12

    You cannot put an imageView inside another. Hope the following code will be helpful for you.

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/background_image"
            android:gravity="center" >
    
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/image_on_center" />
    
        </LinearLayout>
    
    0 讨论(0)
  • 2021-02-10 07:13

    Use RelativeLayout and then use android:layout_centerInParent="true" remember the order you add the ImageViews will decide what is on top :)

    0 讨论(0)
  • 2021-02-10 07:18

    try this

    <?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"
    android:orientation="vertical" >
    
      <RelativeLayout
       android:layout_width="250dp"
       android:layout_height="250dp"  >
    
       <ImageView 
           android:id="@+id/base"
           android:layout_height="250dp"
           android:layout_width="250dp"
           android:src="@drawable/home"
           android:layout_alignParentLeft="true"
           android:layout_alignParentTop="true" />
    
        <ImageView 
           android:id="@+id/center"
           android:layout_height="150dp"
           android:layout_width="150dp"   
           android:src="@drawable/home"
           android:layout_centerInParent="true"
          android:layout_centerVertical="true"   />
     </RelativeLayout>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-02-10 07:24

    Try this:

    android:layout_gravity="center"
    
    0 讨论(0)
提交回复
热议问题