2012-08-14 23 views
14

另一个imageview的顶部这是我的布局,我到目前为止已经试过没有任何成功如何下的ImageView在安卓

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

我究竟想要的是有我inside_imageview的顶部outside_imageview具有确切高度和宽度...如何通过布局来做到这一点?

+0

你尝试加入layout_alignLeft =“@ id/inside_imageview“除了alignTop? – CSmith 2012-08-14 20:04:17

+0

@CSmith是的,我尝试过。 – 2012-08-14 20:10:56

+0

问题是他有一个LinearLayout里面的inside_imageView,所以'layout_alignTop'属性看不到'inside_imageView'的引用ID。如果ID引用是“lltest”,则“outside_imageview”将与LinearLayout对齐。 – DeeV 2012-08-14 20:18:20

回答

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

RelativeLayoutlayout_align[Top|Bottom|Left|Right]属性用于基于内缘各自的x和y值来对准意见。现在,第二个ImageView将根据边距与第一个ImageView的顶部,底部,左侧和右侧对齐。填充在对齐中被忽略。

+0

嗯......其实我有两个更多的imageviews以及inner_imageview ...所以我想到把它们放在一个单独的线性布局中...内部一个我需要做一个水平方向,外部一个我想垂直...所以请在这 – 2012-08-14 20:28:24

+0

建议我这使事情更复杂一点。您无法引用其他视图中的子视图。他们只接受对同级儿童的提及。如果'inside_imageview'和'outside_imageview'具有相同的宽度和高度,那么你只需要将它们放在两个单独的'Linearlayouts'中,并且将两个'LinearLayouts'放在顶部和左边。如果尺寸差异很大,那么您可能必须使用'layout_below'作为另一个,'layout_toRightOf'则使用另一个。 – DeeV 2012-08-14 20:38:03

+0

伟大的答案!爱它 ! – uLYsseus 2014-10-24 05:17:01

6

您应该试试FrameLayout。在FrameLayout中,每个孩子都在彼此之上。

+0

只有一个图像显示在设备上,两个图像,一个在另一个上面正在布局的设计视图中正确显示 – iOSAndroidWindowsMobileAppsDev 2016-10-19 11:46:08

19

FrameLayout是你需要的。您可以简单地合并也是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> 
+2

可悲的是,问这个问题的人,不明白,甚至不想了解什么样的框架布局可以做,并给我们所有人低估。 upvotedü两个家伙:) – 2012-08-23 06:17:24

+0

这甚至减少了一个级别的视图层次结构:woot: – 2013-01-04 07:00:55

+0

好的解决方案。 FrameLayout肯定是要走的路。 – nurxyz 2015-06-29 12:25:55

0

我试过这样的,它为我工作,希望它有助于

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

设备上只显示1个图像,另一个图像显示在另一个图像的上方在版式 – iOSAndroidWindowsMobileAppsDev 2016-10-19 11:47:06

+0

的设计视图中正确显示,所以问题 – 2016-10-19 11:50:44

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

你能解释一下你的代码是干什么的吗? – Alex 2017-03-15 07:13:17

+0

它将imageview放置在另一个imagview的上面......不需要使用任何框架布局 – Sunil 2017-03-15 07:39:45