2016-12-01 6 views
1

私はアンドロイドが新しく、次の問題があります。 画面を4線形レイアウトに分割して、相対レイアウトにするためにルートレイアウトが必要です 私は私の4つのレイアウトを画面上で等しく分割するためにlayout_weightプロパティを使用していましたが、ルートレイアウトを線形レイアウトとして使用した場合にのみ成功しました。 レイアウトXMLは: android layout_weight with 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> 
は、AS(

This is the screen when the root layout is LinearLayout

And this is when it's RelativeLayout(^ Iは、それが示されていない何らかの理由で、最後に 'RelativeLayout' タグ閉鎖しました)

「dp」ユニットを使用して画面を4分割することはできますが、この方法ではいくつかの問題がありました他のもの... 私の主な目標は、あるレイアウトから別のレイアウトにフローティングピクチャをドラッグ&ドロップすることができるようにすることです。そのためにrelativeLayoutを使用する必要があります。さらに、レイアウトを使用してイメージがドロップされたことを知りたいそれによってRectプロパティは何らかの理由で私に偽のポジションを与えました。

ありがとうございます! :)

+1

重みが唯一のLinearLayoutコンテナで動作します。重み付けされた次元は** 0dp **でなければなりません。 –

答えて

0

親の属性センターを持つRelativeLayoutの中間にTextviewを追加すると、topがそのtextViewの上になり、LinearLayoutの下がそのTextviewになり、そのテキストビューを透明にするか、選択(私はurの同じ色を作った)以下のコードを参照してください。

<?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に高さを変更したので、ほとんど見えません –

関連する問題