2015-06-23 54 views
0

我有這樣的代碼:如何把下面的其他佈置的配置在Android的

RelativeLayout frame = new RelativeLayout(activity); 

RelativeLayout layout = new RelativeLayout(this); 
RelativeLayout.LayoutParams dateArea = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT,90 
);     
dateArea.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 

DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics(); 
int height = displayMetrics.heightPixels; 
RelativeLayout mapLayout = new RelativeLayout(this); 
RelativeLayout.LayoutParams mapParam = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT, 
    height-120 
); 

frame.addView(mapLayout,mapParam); 
frame.addView(layout, dateArea); 

我想日期區域底部和地圖下方對齊。但它總是排在頂端,這真的很奇怪,請告訴我這段代碼有什麼問題?

回答

0

做這樣的

RelativeLayout layout = new RelativeLayout(this); 
RelativeLayout.LayoutParams dateArea = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, 
90); 

dateArea .addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
+0

哇!你節省了我的一天(y)。我真的很感激解釋,提前謝謝! – Jerrybb

+0

高興地幫助。祝你有美好的一天。您使用了'FILL_PARENT',因此它佔據了整個屏幕。只是將其更改爲'WRAP_CONTENT',以便獲取實際需要的空間。 –

+0

爲什麼它佔用了整個屏幕而不是填滿父母的寬度? – Jerrybb

0

RelativeLayout可用於相對偏移對其他觀點的任何意見。

  • 你需要定義一個id說res/values/properties.xml(如果這種不存在創建文件或使用任何其他,如果你喜歡)

    <resources> <item name="layout" type="id"/> </resources>

  • 然後分配該ID - layout.setId(R.id.layout)

  • 這種方式你不需要屏幕高度,而是添加另一個規則mapParam.addRule(RelativeLayout.ABOVE, R.id.layout)

然後改變你的代碼:

RelativeLayout.LayoutParams dateArea = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 90); 
dateArea.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
RelativeLayout layout = new RelativeLayout(this); 
layout.setId(R.id.layout); 

RelativeLayout.LayoutParams mapParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 
mapParam.addRule(RelativeLayout.ABOVE, R.id.layout); 
RelativeLayout mapLayout = new RelativeLayout(this); 

RelativeLayout frame = new RelativeLayout(this); 
frame.addView(mapLayout,mapParam); 
frame.addView(layout, dateArea); 
// TODO Don't forget to add 'frame' or it won't show anything 
相關問題