我有一个VideoView,我知道如何将它放在屏幕上,但现在我需要指定dinamically(java代码,而不是xml代码)屏幕上的位置。
这是我的实际代码:
VideoView vv = new VideoView(this);
this.setContentView(vv);
android.widget.FrameLayout.LayoutParams vvParams = (android.widget.FrameLayout.LayoutParams) vv.getLayoutParams();
vvParams.gravity=17; //gravity 17 = center horizontal & vertical
vvParams.width=150;
vv.setLayoutParams(vvParams);
String fileName = "android.resource://" + getPackageName() + "/" + R.raw.v;
vv.setVideoURI(Uri.parse(fileName));
vv.start();
这些是我想指定的位置:
(垂直/水平)
如何使用java代码以dinamically方式指定这些位置?
答案 0 :(得分:3)
让你的VideoView成为RelativeLayout
的孩子,然后像这样更新LayoutParams:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams();
// Horizontal alignment - choose as appropriate
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
//lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// Vertical alignment - choose as appropriate
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//lp.addRule(RelativeLayout.CENTER_VERTICAL);
//lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
vv.setLayoutParams(lp);