2016-11-15 61 views
1

我加了View到我的RelativeLayout,我設置了300dp保證金,剩下 然後我又增加了一個視圖,但第二個視圖不受第一個保證金的影響。爲什麼?RelativeLayout和margin?

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

    <TextView 
     android:id="@+id/text" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:layout_marginLeft="300dp" 
     android:background="@color/colorPrimaryDark" 
     android:text="use margin left view 1" 
     android:textSize="40sp" /> 

    <TextView 
     android:layout_width="290dp" 
     android:layout_height="200dp" 
     android:background="@color/colorPrimaryDark" 
     android:text="not affected by margin view 2 " 
     android:textSize="40sp" /> 
</RelativeLayout> 

結果:

enter image description here

回答

0

這是因爲你已經把他們在您需要錨孩子彼此的意見相對於RelativeLayout

所以在這種情況下,您必須將屬性android:layout_toRightOf=´="@+id/text"添加到您的第二個TextView

整個佈局看起來就像這樣:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:text="Some text" 
     android:layout_marginLeft="300dp" 
     android:textSize="40sp" 
     android:background="@color/colorPrimaryDark"/> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_toRightOf=="@+id/text1" 
     android:layout_width="290dp" 
     android:layout_height="200dp" 
     android:text="Some other text" 
     android:textSize="40sp" 
     android:background="@color/colorPrimaryDark"/> 

</RelativeLayout> 

你可以閱讀更多有關的RelativeLayout這裏https://developer.android.com/guide/topics/ui/layout/relative.html

0

這是因爲你只適用marginLeft到第一TextView,不是他們兩個。如果您想將marginLeft同時適用於TextViews,你可以用LinearLayout圍繞着他們兩個,然後給LinearLayout的300dp一個marginLeft或者你可以給雙方TextViews一個marginLeft的300dp

下面是代碼的包裝都在TextViews一個LinearLayout

<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     //Other stuff here 

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="300dp"> 

     <TextView 
       android:layout_width="200dp" 
       android:layout_height="200dp" 
       android:text="use margin left view 1" 
       android:id="@+id/text" 
       android:textSize="40sp" 
       android:background="@color/colorPrimaryDark"/> 

     <TextView 
       android:layout_width="290dp" 
       android:layout_height="200dp" 
       android:text="not affected by margin view 2 " 
       android:textSize="40sp" 
       android:background="@color/colorPrimaryDark"/> 
    </LinearLayout> 

    //Other stuff here 
</RelativeLayout> 

你也可以只使用一個RelativeLayout的屬性來獲取諸如alignLeft相同的期望結果。

下面是代碼:

<RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

    <TextView 
      android:id="@+id/text" 
      android:layout_width="200dp" 
      android:layout_height="200dp" 
      android:layout_marginLeft="300dp" 
      android:text="use margin left view 1" 
      android:textSize="40sp" 
      android:background="@color/colorPrimaryDark"/> 

    <TextView 
      android:layout_width="290dp" 
      android:layout_height="200dp" 
      android:layout_alignLeft="@id/text" 
      android:text="not affected by margin view 2 " 
      android:textSize="40sp" 
      android:background="@color/colorPrimaryDark"/> 

</RelativeLayout> 

其他RelativeLayout屬性,嘗試可以在這裏找到:https://developer.android.com/reference/android/widget/RelativeLayout.html

,這裏是一些正在使用的例子: https://developer.android.com/guide/topics/ui/layout/relative.html

0

RelativeLayout是容器。它不會自動將佈局參數從一個孩子傳遞給另一個孩子。

要在RelativeLayout所有的孩子都有相同的左緣的話,我會建議保證金添加到RelativeLayout本身,而不是TextView

所以,你的代碼是:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_marginLeft="300dp" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="200dp" 
     android:layout_height="200p" 
     android:text="text 1" 
     android:background="@color/colorPrimaryDark"/> 

    <TextView 
     android:layout_width="290dp" 
     android:layout_height="200dp" 
     android:text="text 2" 
     android:background="@color/colorPrimaryDark"/> 

</RelativeLayout> 
0

這是在這種情況下更好地使用LinearLayout。不要忘記提及方向:

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

<TextView 
    android:id="@+id/text" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_marginLeft="300dp" 
    android:background="@color/colorPrimaryDark" 
    android:text="use margin left view 1" 
    android:textSize="40sp" /> 

<TextView 
    android:layout_width="290dp" 
    android:layout_height="200dp" 
    android:background="@color/colorPrimaryDark" 
    android:text="affected by margin view 2 " 
    android:textSize="40sp" /> 

</LinearLayout>