在我的Android应用程序中(用于横向屏幕方向)我需要将小部件放置到两个相对布局中,一个位于屏幕左侧,一个位于右侧(以填充完整大小).
我更喜欢以编程方式工作(我发现它比xml更灵活).
Shoud我最好使用TableLayout作为我的子布局的父布局?
小智 11
只有两个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>
Run Code Online (Sandbox Code Playgroud)
编辑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);
Run Code Online (Sandbox Code Playgroud)