2011-08-14 56 views

回答

10

只有兩個RelativeLayouts彼此相鄰,你有很多選擇來實現這一點。在我看來,水平的LinearLayout是最簡單的。


編輯:我從來不做佈局中的代碼,但因爲你可能看了很多與XML文檔的,你應該能夠把這種例子。兩種佈局都使用50/50空間分配。

<LinearLayout android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal"> 
    <RelativeLayout android:layout_width="0dp" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" > 

    </RelativeLayout> 

    <RelativeLayout android:layout_width="0dp" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" > 

    </RelativeLayout> 

</LinearLayout> 

編輯2:

肯定的作品,只是嘗試這樣做:

LinearLayout layoutContainer = new LinearLayout(this); 
layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

// Arguments here: width, height, weight 
LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT, 1); 

RelativeLayout layoutLeft = new RelativeLayout(this); 
layoutContainer.addView(layoutLeft, childLp); 

RelativeLayout layoutRight = new RelativeLayout(this); 
layoutContainer.addView(layoutRight, childLp); 
+0

代碼,請... :-) – MarcoS

+1

既然你喜歡編程工作,看看在LinearLayout.LayoutParams:HTTP ://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html – glorifiedHacker

+0

孩子們的RelativeLayouts不應該使用fill_parent的寬度,而是使用0dip。 –

0

回答我的問題:

的方法通過alextsc建議沒有工作,因爲RelativeLayouts(與LinearLayouts相反)沒有任何權重。

我沒有這個(醜:-()解決黑客:

LinearLayout layoutContainer = new LinearLayout(myActivity.this); 
layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

int width = getWindowManager().getDefaultDisplay().getWidth()/2; 

RelativeLayout layoutLeft = new RelativeLayout(Results.this); 
layoutContainer.addView(layoutLeft, width, LayoutParams.FILL_PARENT); 

RelativeLayout layoutRight = new RelativeLayout(Results.this); 
layoutContainer.addView(layoutRight, width, LayoutParams.FILL_PARENT); 
+0

正如我在上面的評論所述,layout_weight涉及外部LinearLayout。檢查我的第二個編輯。這和我的xml在代碼中做的是一樣的,事實上它工作正常。 – 2011-08-14 16:34:43

+0

@alextsc:對不起,它完美的工作,我不明白我可以使用LinearLayout.LayoutParams添加一個RelativeLayout ...感謝您的答案。 – MarcoS

+0

沒問題,這就是爲什麼我發佈它。 :) – 2011-08-14 21:37:04

相關問題