如何指定视频的位置? (左上,右上,左下......等)

时间:2011-11-29 16:05:53

标签: android android-layout android-videoview

我有一个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方式指定这些位置?

1 个答案:

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