views:

347

answers:

1

I want a TextView on top and a VideoView below it. I want to have the VideoView as center verical and below the TextView. I am using RelativeLayout.

I am trying to do this in code:

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);

The VideoView gets placed below the TextView, but the VideoView is not center vertical.

A: 

You made your VideoView fill_parent/fill_parent, which gives it the same dimensions as its parent RelativeLayout. If it's as big as the RelativeLayout, centering cannot really work. Also, you cannot have the VideoView be both below the TextView and centered vertically in the parent; it's one or the other. Instead you could center the ViewView and place the TextView above it.

Romain Guy
The reason I used fill_parent/fill_parent for videoview is that I want the video to fill all the space below TextView. The video will only scale upto what its aspect ratio allows it, and that is why the need for centering, especially in Portrait mode.
Chris