2012-10-16 14 views
4

Salut!我在relativelayout中遇到問題,其子代可以使用layout_below和layout_weight設置其百分比高度。RelativeLayout和height百分比。無法使用layout_below和layout_weight

例如。我們有相對的佈局,裏面有3個帶textviews的線性佈局。現在,我想爲它們中的3個(線性佈局)設置相對佈局總高度的25,50.25%。我怎麼能做到這一點?

我找到了weight_sum and and relativelayout (CLICK)的解決方案,但它沒有使用layout_below。

請幫忙。我試圖在所有線性佈局中添加額外的線性佈局,但之後我無法使用layout_below。

這裏是概念PIC的想法: enter image description here

這裏是我的代碼:

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_below="@+id/linlay_leftcol_notification_header" > 

<LinearLayout 
android:id="@+id/linlay_leftcol_clock_theday" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" > 

<TextView 
    android:id="@+id/tv_theday" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="@string/theday" 
    android:textColor="#FFFFFF" /> 
</LinearLayout> 

<LinearLayout 
android:id="@+id/linlay_leftcol_clock_thetime" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_below="@+id/linlay_leftcol_clock_theday" > 

<TextView 
    android:id="@+id/tv_thetime" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="@string/thetime" 
    android:textColor="#FFFFFF" /> 
</LinearLayout> 

<LinearLayout 
android:id="@+id/linlay_leftcol_clock_thenumber_themonth_theyear" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_below="@+id/linlay_leftcol_clock_thetime" > 

<TextView 
    android:id="@+id/tv_thenumberofday_themonth_theyear" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="@string/thenumberofday_themonth_theyear" 
    android:textColor="#FFFFFF" /> 
</LinearLayout> 
</RelativeLayout> 

附:請原諒語法,因爲我仍然在學習這種語言。我已經在發佈之前尋找解決方案。

回答

10

試試這個.. 。希望這能解決的目的.. :-)

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

    <RelativeLayout 
     android:layout_weight="1" 
     android:background="#ffff00" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/textView0" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="Hello 1" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_weight="2" 
     android:background="#00ffff" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="Hello 2" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_weight="1" 
     android:background="#ffff00" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="Hello 3" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 
    </RelativeLayout> 
</LinearLayout> 
+0

-1,因爲我說的話有關百分比高度 – deadfish

+0

如果你看看內3個佈局的緊密我一直保持權重1,2和1(按順序)保持期望的輸出,即第一個佈局將佔據顯示高度的25%,第二個將佔用50%,第三個將保留25%。對我來說工作得很好。 – Gagan

+0

試用後,如果您對此答案感到滿意,那麼可能是您想要撤銷您的'-1':-) – Gagan

0

您可以使用任一重量或相對佈局屬性。你可以使25,50,25這個...你將父對齊第一個textview,對齊父權,其他textview,和你對齊第一個左邊的剩餘文本視圖和右邊的最後一個。

0

使用表格佈局,並在裏面,你把你的textviews。有一個屬性「伸展列」表格佈局,有那麼你可以說大小爲「0,2,1」這樣的..

3

檢查這個::

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

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight=".15" 
     android:background="@android:color/background_dark" 
     android:orientation="vertical" > 

     <RelativeLayout 
      android:id="@+id/rel_1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#A52A2A" > 

      <TextView 
       android:id="@+id/tv_theday" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:text="1st layout" 
       android:textColor="#FFFFFF" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/rel_2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#8B008B" > 

      <TextView 
       android:id="@+id/tv_theday" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:text="2ndlayout" 
       android:textColor="#FFFFFF" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/rel_3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#00008B" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:text="3rd layout" 
       android:textColor="#FFFFFF" /> 
     </RelativeLayout> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/rel_side" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight=".75" 
     android:background="#ff0000" > 

     <TextView 
      android:id="@+id/tv_theday" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="side layout" 
      android:textColor="#FFFFFF" /> 
    </RelativeLayout> 

</LinearLayout>