2016-12-01 117 views
1

我是新來的android和我有以下問題: 我想分裂我的屏幕4線性佈局和我需要根佈局是相對佈局, 我試圖使用layout_weight屬性爲了在屏幕上同樣地分割我的4佈局,但是我只能成功地做到這一點,當我使用根佈局作爲線性佈局。 佈局XML:android layout_weight與RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/view_root" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_weight="1" 
android:orientation="vertical" 
android:splitMotionEvents="false"> 

<LinearLayout 
    android:id="@+id/top_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

    <LinearLayout 
     android:id="@+id/a_status_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#5080ce"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="A status"/> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/b_status_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#356dc6"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="B status"/> 

    </LinearLayout> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/bottom_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    > 

    <LinearLayout 
     android:id="@+id/c_status_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#325287"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="C status"/> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/d_status_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#26477c" > 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="D status"/> 
    </LinearLayout> 
</LinearLayout> 

(^ I關閉 'RelativeLayout的' 標籤到底,由於某種原因,它未示出)

This is the screen when the root layout is LinearLayout

And this is when it's RelativeLayout(如在上面的xml)

我可以使用'dp'單位將屏幕分割爲4,但這樣我就遇到了問題其他的東西...... 我的主要目標是能夠將浮動圖片從一種佈局拖放到另一種佈局,我需要使用relativeLayout,另外我想知道在哪種佈局下圖像已被丟棄,通過使用由於某些原因,它的屬性給了我虛假的位置。

非常感謝你! :)

+1

重量只有在的LinearLayout容器的工作。加權的維度必須是** 0dp **。 –

回答

0

只需在兩個RelativeLayout的中間添加一個文本視圖,並將屬性中心添加到父視圖中,並且頂部將位於該文本視圖的上方,而LinearLayout下方將位於該文本視圖的下方,並且您可以使該文本視圖透明,選擇(我做了與你提到的相同的顏色)看到下面的代碼。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/view_root" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_weight="1" 
android:orientation="vertical" 
android:splitMotionEvents="false"> 

<LinearLayout 
    android:id="@+id/top_layout" 
    android:layout_above="@+id/tv_dummy" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

    <LinearLayout 
     android:id="@+id/a_status_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#5080ce"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="A status" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/b_status_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#356dc6"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="B status" /> 

    </LinearLayout> 
</LinearLayout> 

<TextView 
    android:id="@+id/tv_dummy" 
    android:layout_centerInParent="true" 
    android:background="#5080ce" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<LinearLayout 
    android:id="@+id/bottom_layout" 
    android:layout_below="@+id/tv_dummy" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

    <LinearLayout 
     android:id="@+id/c_status_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#325287"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="C status" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/d_status_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#26477c"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="D status" /> 
    </LinearLayout> 
</LinearLayout> 

enter image description here

+0

謝謝!我改變了高度爲1dp,所以它幾乎看不見 –