2011-08-24 31 views
1

我試圖將TextView dependent置於RelativeLayout內的其他TextView anchor之上,但我無法設法使dependent得到顯示。RelativeLayout:如果涉及保證金,則不能在視圖上方分配視圖

現狀:
anchor將與家長的頂部對齊+一些marginTop使其更父(RelativeLayout的)的中心,和dependent將對齊是上述的這anchor

這是行不通的;當我將它分配到anchor以上時,似乎android假定anchor的頂部是父級的頂部,並在屏幕之外(在其上方)繪製dependent

這不應該是這樣,因爲我使用的保證金代替填充所以RelativeLayout的頂部和anchor之間的區域不應該是anchor本身的一部分(我檢查與層次結構的大小觀衆)。或者,也許我錯了? :S

這是一個簡單的佈局代碼:

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

    <TextView android:id="@+id/anchor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="100px" 
     android:text="Anchor point." 
     /> 
    <TextView android:id="@+id/dependent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/anchor" 
     android:text="Dependent." 
     /> 

</RelativeLayout> 

期望:

 
------------- 
|   | 
|dependent | 
|anchor  | 
|   | 
------------- 

發生了什麼事:

 
dependent (out of screen display) 
------------- 
|   | 
|   | 
|anchor  | 
|   | 
------------- 

希望:
有人可以幫我嗎?或者,也許可以幫助指出我是否犯了一個錯誤。我需要在我的真實實現中使用RelativeLayout(以上只是一個示例)。

在此先感謝。

+0

你有沒有想過這個?我遇到了同樣的問題 –

+0

對不起,我沒有注意到你的評論。不,我不知道這裏出了什麼問題。最後,我用一個空洞的視圖作爲填充來替換錨的邊緣。 –

+0

是的,我最終從另一個視圖的底部保留底部 –

回答

0

我可以問你爲什麼不把錨定在最上面的角落(頂部或底部 - 無所謂),然後建立你的觀點?這就是我所做的,那是如下:對不起 - 我現在還不能張貼照片。

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

    <TextView android:id="@+id/anchor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="100dp" 
     android:text="Anchor point." 
    /> 
    <TextView android:id="@+id/dependent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/anchor" 
     android:layout_alignLeft="@id/anchor" 
     android:text="Dependent." 
    /> 

</RelativeLayout> 

或者,如果你真的想保持事物的本來面目,只是改變的依賴對齊到Android:layout_alignParentTop =「真」。您也可以在此處設置保證金以影響其展示位置。這是代碼和圖片。

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

<TextView android:id="@+id/anchor" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="100dp" 
    android:text="Anchor point." 
    /> 
<TextView android:id="@+id/dependent" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="150dp" 
    android:text="Dependent." 
    /> 

</RelativeLayout> 
+0

使用父親的頂部或底部作爲錨點並非我想要的使用RelativeLayout。假設我們正在製作一個類似facebook的評論,其中有用戶名爲Dependent,評論爲Content,當有佈局對齊時,我不想更改每個視圖的邊距。只有錨和其他應該遵循.. –

+0

順便說一句我看不出你的代碼和我的區別,除了layout_below和layout_alignLeft。他們應該給我和我一樣的結果,只是略有不同。 –

+0

在上面的代碼中沒有任何東西在屏幕上消失。沒有依賴迷路的問題。我不遵循你的評論「假設我們做了一個類似facebook的評論,其中有用戶名爲Dependent,評論爲Content,當有一個佈局對齊時,我不想改變每個視圖的邊距只有錨和其他的應該跟隨。「 – DLD

1

我做了一個看不見的按鈕來對齊 - 這是你在找什麼?如果您更改邊距或錨按鈕的任何位置參數,它將更改依賴項的位置。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<Button android:id="@+id/anchorbutton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="100dp" 
    android:visibility="invisible" 
/> 

<TextView android:id="@+id/anchor" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/anchorbutton" 
    android:layout_alignLeft="@id/anchorbutton" 
    android:layout_margin="10dp" 
    android:text="Off Anchor button" 
/> 
<TextView android:id="@+id/dependent" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@id/anchorbutton" 
    android:layout_below="@id/anchor" 
    android:layout_margin="50dp" 
    android:text="Dependent."