2010-07-24 82 views
3

我想要一個TextView頂部和它下面的VideoView。我想要VideoView作爲中心verical和低於TextView。我正在使用RelativeLayoutAndroid - RelativeLayout CENTER_VERTICAL和BELOW問題

我試圖做到這一點代碼:

RelativeLayout layout = new RelativeLayout(this); 
TextView tv = new TextView(this); 
tv.setText("Hello"); 
tv.setGravity(Gravity.RIGHT); 
tv.setTextColor(Color.WHITE); 
tv.setId(1); 

RelativeLayout.LayoutParams one = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

one.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
one.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 


RelativeLayout.LayoutParams two = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
two.addRule(RelativeLayout.CENTER_VERTICAL); 
two.addRule(RelativeLayout.BELOW, 1); 

VideoView mVideoView = new VideoView(this); 

layout.addView(tv, one); 
layout.addView(mVideoView, two); 

setContentView(layout); 

VideoView被放置在TextView下方,但VideoView沒有垂直居中。

回答

1

您製作了您的VideoView fill_parent/fill_parent,這使其具有與其父親RelativeLayout相同的尺寸。如果它與RelativeLayout一樣大,則居中不能真正起作用。此外,您不能讓VideoView既不在TextView下面,也不在父視圖中垂直居中;它是一個或另一個。相反,您可以將ViewView居中並將TextView放在它上面。

+0

我對videoview使用fill_parent/fill_parent的原因是我希望視頻能夠填充TextView下面的所有空間。視頻只能根據其縱橫比允許的比例進行擴展,這就是爲什麼需要進行對中,尤其是在縱向模式下。 – Chris 2010-07-25 00:18:18