在我的Android应用程序中(针对横向屏幕方向),我需要将小部件放置到两个相对布局,一个在屏幕左侧,另一个在右侧(填充完整大小)。Android,如何放置两个相对的布局,一个在左边,一个在屏幕的右边?
我更喜欢以编程方式工作(我发现它比xml更灵活)。
我最好使用TableLayout作为我的子布局的父布局?
在我的Android应用程序中(针对横向屏幕方向),我需要将小部件放置到两个相对布局,一个在屏幕左侧,另一个在右侧(填充完整大小)。Android,如何放置两个相对的布局,一个在左边,一个在屏幕的右边?
我更喜欢以编程方式工作(我发现它比xml更灵活)。
我最好使用TableLayout作为我的子布局的父布局?
只有两个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);
回答我的问题:
的方法通过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);
正如我在上面的评论所述,layout_weight涉及外部LinearLayout。检查我的第二个编辑。这和我的xml在代码中做的是一样的,事实上它工作正常。 – 2011-08-14 16:34:43
@alextsc:对不起,它完美的工作,我不明白我可以使用LinearLayout.LayoutParams添加一个RelativeLayout ...感谢您的答案。 – MarcoS
没问题,这就是为什么我发布它。 :) – 2011-08-14 21:37:04
代码,请... :-) – MarcoS
既然你喜欢编程工作,看看在LinearLayout.LayoutParams:HTTP ://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html – glorifiedHacker
孩子们的RelativeLayouts不应该使用fill_parent的宽度,而是使用0dip。 –