在我的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。 –