2014-02-12 75 views
3

如何將帶有java代碼的視頻視圖添加到xml文件?如何以編程方式添加視頻視頻

+0

你不能將它添加到java代碼的xml文件中,但是你可以在代碼中將它添加到運行時間通過動態添加它到佈局 –

+1

可以請你在這裏發佈一個代碼示例嗎?謝謝 – user3042700

+0

好的給我點時刻 –

回答

4

這裏是我的例外,你是在你的XML佈局使用RelativeLayout

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout); 
    VideoView video = new VideoView(this); 
    video.setVideoURI("yourURI"); 
    video.setLayoutParams(new FrameLayout.LayoutParams(550, 550)); 
    layout.addView(video); 

隨意餵我在什麼事上並不明顯,因爲我已經使用了這樣的事情,你

+0

謝謝mohammed momn!我會嘗試這些代碼,然後給我回報你!再次感謝; D – user3042700

+0

嗨,再次穆罕默德媽媽。你給我的代碼工作正常!現在我有另一個問題。我想給我的videoview或framelayout「風格」,所以我可以把它放在我的應用程序的正確位置。我曾經使用類似的東西: - LayoutParams rp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); - rp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); - cb1.setLayoutParams(rp); – user3042700

+0

現在我怎樣才能做到這一點: final LinearLayout lm =(LinearLayout)findViewById(R.id.linearMain); RelativeLayout rl = new RelativeLayout(this); VideoView video = new VideoView(this); video.setVideoPath(「http://tgdsrv.dyndns.org/kroosky/uploads/VID_20140128_215547.mp4」); video.setLayoutParams(new FrameLayout.LayoutParams(200,200)); MediaController mediacontroller = new MediaController(VideoDynamic.this); mediacontroller.setAnchorView(video); video.setMediaController(mediacontroller); rl.addView(video); \t lm.addView(rl); – user3042700

0

代碼

final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain); 

RelativeLayout rl = new RelativeLayout(this); 

ImageView cb1 = new ImageView(this); 
cb1.setId(1); 
cb1.setImageResource(R.drawable.ic_launcher); 

ImageButton cb2 = new ImageButton(this); 
cb2.setImageResource(R.drawable.ic_launcher); 
cb2.setBackground(null); 
cb2.setId(2); 

LayoutParams rp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

rp.addRule(RelativeLayout.BELOW, cb1.getId()); 

cb2.setLayoutParams(rp); 

rl.addView(cb1); 
rl.addView(cb2); 

lm.addView(rl); 

現在我想要做相同的,但在此代碼,這就是你給我的一個:

final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain); 

//Create views 
RelativeLayout rl = new RelativeLayout(this); 

//TEXTVIEW 
TextView txv = new TextView(this); 
txv.setText("TEEEEEEEEEXT"); 
txv.setId(1); 
//TEXTVIEW 

//VIDEOVIEW 
VideoView video = new VideoView(this); 
video.setId(2); 

      video.setVideoPath("http://tgdsrv.dyndns.org/kroosky/uploads/VID_20140128_215547.mp4"); 

     android.widget.FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams(300, 300); 
     video.setLayoutParams(fl); 

     //Create video controllers 
     MediaController mediacontroller = new MediaController(VideoDynamic.this); 
     mediacontroller.setAnchorView(video); 

     //Set controllers to video 
     video.setMediaController(mediacontroller); 
    //VIDEOVIEW  

     rl.addView(txv); 
     rl.addView(video); 

     lm.addView(rl); 
相關問題