2012-12-23 60 views
12

我確實有3個RelativeLayout,它位於main_RelativeLayout中。該視圖將是垂直的。所以,我有3個RelativeLayout,它們相鄰設置,我希望它們填滿整個屏幕,不管屏幕大小如何。我,佈局視圖:RelativeLayout權重

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/backg" 
> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="@dimen/top_mr_image" 
    android:src="@drawable/temp" /> 

<RelativeLayout 
    android:id="@+id/r1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/imageView1" 
    android:layout_marginLeft="10dp" 
    android:background="@drawable/r1bg" 
    android:layout_weight="1" 

    > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="@dimen/txt_mr_right" 
     android:layout_marginRight="@dimen/txt_mr_right" 
     android:layout_marginTop="39dp" 
     android:text="S" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/textView1" 
     android:layout_marginLeft="@dimen/txt_mr_right" 
     android:layout_marginRight="@dimen/txt_mr_right" 
     android:text="T" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
</RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/r2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignTop="@+id/r1" 
     android:layout_toRightOf="@+id/r1" 
     android:layout_weight="1" > 
    </RelativeLayout> 

      <RelativeLayout 
     android:id="@+id/r3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignTop="@+id/r2" 
     android:layout_toRightOf="@+id/r2" 
     android:layout_weight="1" > 

    </RelativeLayout> 

我設置weight=1layout_width=0dp每個releativeLayout這種技術的工作原理與按鈕,我認爲同樣將與RelativeLayout的,看來我的想法是錯誤的。任何想法?

UPD1:我添加的我想什麼的圖像具有

enter image description here

+0

我所做的事,我一直在尋找,謝謝你的意見 – Daler

回答

20

RelativeLayout不注意android:layout_weight。 (這是LinearLayout.LayoutParams的財產,但不是RelativeLayout.LayoutParams。)

您應該能夠以更簡單的視圖層次結構獲得所需的佈局。由於最後兩個RelativeLayout是空的,因此不清楚你在做什麼。如果你需要一個純粹的垂直組織,我建議使用LinearLayout而不是RelativeLayout

編輯根據您的編輯,看起來您需要三個複合視圖的水平佈局,每個視圖都可點擊。我認爲像下面的工作:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <!-- First column --> 
    <LinearLayout 
     android:id="@+id/firstColumn" 
     android:orientation="vertical" 
     android:gravity="center_horizontal" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="..." /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="text 1" 
      . . . /> 
    </LinearLayout> 

    <!-- Second column --> 
    <LinearLayout . . . > 
     . . . 
    </LinearLayout> 
</LinearLayout> 

如果按鈕的內容是不正確的,可以更換第二級與RelativeLayoutLinearLayout意見是否可以幫助組織更好地佈局。

+0

我確實有relativeLayout裏面的元素必須以確切的方式進行定位,它將是一種圖像等自定義clickalble元素。由於我的編碼知識很差 – Daler

+0

@Daler - 如果您提供了有關您想要的佈局的更多細節(草圖或屏幕截圖會很棒),我們可以幫助您實現它。還要注意,一個垂直的'LinearLayout'可以包含嵌套的佈局,以在任何需要它的行上提供水平組織。 –

+0

感謝您的反饋,我已更新我的帖子,你可以看到我想要的。我想讓每個relativeLayout可點擊,所以它們將作爲一個按鈕。所有三個人都有不同的圖像類型和文本相同的設計 – Daler

4

RelativeLayouts不支持的重量。如果你想使用權重,你需要使用LinearLayout作爲父容器。

+0

如果家長什麼的RelativeLayout?我可以在linearLayout中添加其餘的relativeLayouts。有點硬核,但可能 – Daler

+0

我想你可以有一個RelativeLayout - > LinearLayout - > RelativeLayout x 3,但我不知道那會完成。 – dmon

0

解決方案非常簡單。我一直在尋找相對佈局的重量分佈。

這對所有這些類型的情況來說都是一個小竅門。

使用的LinearLayout與機器人:方向=「橫向」