How to place an imageview on top of another imageview in android

馋奶兔 提交于 2019-11-26 18:23:32

问题


This is my layout which i tried so far without any success

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white">

 <LinearLayout 
    android:id="@+id/lltest" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_centerHorizontal="true">

        <ImageView 
        android:id="@+id/inside_imageview" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginTop="5dip"
        android:layout_marginBottom="5dip"
        android:src="@drawable/frame"/>

</LinearLayout>

 <ImageView 
        android:id="@+id/outside_imageview" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignTop="@id/inside_imageview"
        android:scaleType="fitXY"/>
</RelativeLayout>

What i exactly want is to have my outside_imageview on top of inside_imageview with the exact height and width... How to do it through layout?


回答1:


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

    <ImageView
        android:id="@+id/inside_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="5dip"
        android:src="@drawable/frame" />

      <ImageView
         android:id="@+id/outside_imageview"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignTop="@id/inside_imageview"
         android:layout_alignBottom="@id/inside_imageview"
         android:layout_alignLeft="@id/inside_imageview"
         android:layout_alignRight="@id/inside_imageview"            
         android:scaleType="fitXY" />
  </RelativeLayout>

The layout_align[Top|Bottom|Left|Right] attribute in RelativeLayout is used to align views based on their respective x and y values within the margin. The second ImageView will now be aligned to the top, bottom, left, and right of the first ImageView based on the margins. Padding is ignored in the alignment.




回答2:


FrameLayout is what you need. You can simply merge the parent layout that is a FrameLayout too. Take a look at the Android Developers Blog: http://android-developers.blogspot.it/2009/03/android-layout-tricks-3-optimize-by.html

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

    <ImageView
        android:id="@+id/outside_imageview" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"/>

    <ImageView
        android:id="@+id/inside_imageview" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginTop="5dip"
        android:layout_marginBottom="5dip"
        android:src="@drawable/frame" />
</merge>



回答3:


You should try a FrameLayout. Inside a FrameLayout every Child is on top of each other.




回答4:


I tried this way and it worked for me,hope it helps

 <FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dip">
<ImageView
    android:id="@+id/image_first"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/imga"/>
<ImageView
    android:id="@+id/imageview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/image"/>
</FrameLayout>



回答5:


                <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="@dimen/_15dp">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="@dimen/_10dp"
                    android:layout_marginTop="@dimen/_50dp"
                    android:background="@drawable/white_background"
                   />
                <ImageView
                    android:layout_width="@dimen/_100dp"
                    android:layout_height="@dimen/_100dp"
                    android:background="@drawable/my_credit"
                    android:layout_marginTop="@dimen/_5dp"
                    android:layout_centerHorizontal="true"
                    />
            </RelativeLayout>



回答6:


I use Elevation only... 0=floor or basement ... lol 1=floor2 10=floor10 the top top

    <ImageView
          android:id="@+id/blog_user_image_gp"
          android:layout_width="match_parent"
          android:layout_height="200dp"
          android:layout_marginTop="70dp"
          android:src="@drawable/avatar"
          android:scaleType="fitXY"
          android:foregroundGravity="center"
          android:elevation="10dp"
          />
  <android.support.v7.widget.RecyclerView
        android:id="@+id/users_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:elevation="1dp"
        android:scrollbars="vertical" />



回答7:


Just add translation z and it will be on top, make sure the translation z is greater than the view you want to be on top of. Sorry for the late response.



来源:https://stackoverflow.com/questions/11959841/how-to-place-an-imageview-on-top-of-another-imageview-in-android

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