2011-08-14 37 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

相关问题