簡體   English   中英

RelativeLayout:如果涉及邊距,則無法在參考視圖上方分配視圖

[英]RelativeLayout: Can't assign a view above a referenced view if margin is involved

我試圖將一個TextView dependent放在RelativeLayout內的另一個TextView anchor上方,但是我無法設法使該dependent得到顯示。

情況:
anchor將與父節點的top +一些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(上面只是一個示例)。

提前致謝。

我做了一個不可見的按鈕來對齊-這是您要找的東西嗎? 如果更改margin或anchorbutton的任何位置參數,它將更改受撫養人的位置。

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

請問為什么您不將錨定在最上角(頂部或底部-沒關系),然后從中建立視圖? 那是我的工作,其內容如下:對不起-我還不能發布圖片。

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

另外,如果您確實想保持原樣,則只需將dependent的對齊方式更改為android:layout_alignParentTop =“ true”。 您也可以在此處留有余量來影響其位置。 這是代碼和圖片。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM