2012-08-14 30 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