23

这是我的布局,到目前为止我尝试过但没有成功

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

我真正想要的是让我的outside_imageview 位于inside_imageview 之上,并具有精确的高度和宽度......如何通过布局来做到这一点?

4

7 回答 7

44
    <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>

layout_align[Top|Bottom|Left|Right]in 属性用于根据RelativeLayout页边距内各自的 x 和 y 值对齐视图。第二个现在将根据边距ImageView与第一个的顶部、底部、左侧和右侧对齐。ImageView对齐中忽略填充。

于 2012-08-14T20:05:20.790 回答
20

FrameLayout是你需要的。您可以简单地合并也是 a 的父布局FrameLayout。看看Android 开发者博客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>
于 2012-08-14T20:13:59.300 回答
6

你应该尝试一个FrameLayout. 在 FrameLayout 中,每个 Child 都在彼此之上。

于 2012-08-14T20:04:41.390 回答
3

我试过这种方式,它对我有用,希望它有帮助

 <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>
于 2016-09-01T09:05:27.477 回答
3

我只使用高程... 0=地板或地下室...大声笑 1=地板 2 10=地板 10 顶部

    <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" />
于 2019-10-25T14:26:37.817 回答
1
                <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>
于 2017-03-15T06:57:20.397 回答
-1

只需添加翻译 z ,它就会在顶部,确保翻译 z 大于您想要位于顶部的视图。回复晚了非常抱歉。

于 2018-04-15T13:55:19.367 回答