2011-11-29 94 views
0

我有一個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(); 

這些都是可能的位置,我想說明:

(垂直/水平)

  • 左上角,頂部中心,右上
  • 左中心,中心右中心
  • 左下,博tom中心,右下角

如何用java代碼指定這些possitions?

回答

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); 
相關問題