我正嘗試在同一屏幕上創建一個包含視頻播放和兩個ImageButton的視圖。我使用VideoView來包含我的兩個按鈕的視頻和ImageButton。當我單獨測試每個功能時,它們都能正確顯示,但當我嘗試在同一屏幕上顯示時,它們不會同時顯示在一起!我已經嘗試了許多佈局(框架,線性,相對),將VideoView限制爲更小的layout_width,並嘗試了xml文件中的權重,但似乎沒有任何效果。顯示這似乎太簡單了,需要一個自定義視圖,但我會做到這一點,如果我必須。在Android中顯示視頻和按鈕在同一屏幕上
這裏是我的問題:你知道如何使imageButtons和VideoView顯示在同一屏幕上嗎? 當您創建自定義視圖時,您可以使用Android視圖,如VideoView和ImageButton嗎?或者你可以在自定義視圖中的畫布上繪製2D事物嗎?
這裏是我的代碼作爲參考:XML
<LinearLayout
android:id="@+id/videopart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1">
<VideoView
android:id="@+id/videoplayer"
android:layout_width="395dp"
android:layout_height="111dp" >
</VideoView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<ImageButton
android:contentDescription="@string/top"
android:id="@+id/topbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dip"
android:src="@drawable/yesbutton"
android:background="@null"/>
<ImageButton
android:contentDescription="@string/bottom"
android:id="@+id/bottombutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:src="@drawable/nobutton"
android:background="@null">
</ImageButton>
</LinearLayout>
和活動:
public class iplayer extends Activity {
VideoView videoHolder;
MediaPlayer mp;
ImageButton top, bottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.interactivevid);
videoHolder.setVideoURI(video);
// video finish listener:
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// The video has finished, return from this activity
finish(); //close activity
}
});
videoHolder.requestFocus();
/*
videoHolder.requestLayout();
videoHolder.invalidate();
videoHolder.getLayoutParams().width = 20;//480;
*/
//start video
drawButtons();
//videoHolder.setPadding(BIND_NOT_FOREGROUND, BIND_NOT_FOREGROUND, BIND_NOT_FOREGROUND, BIND_NOT_FOREGROUND);
videoHolder.setPadding(5, 0, 5, 200);
setContentView(videoHolder); // used to actually put in video. when removed, shows buttons
videoHolder.start();
}
private void drawButtons(){
//make buttons invisible
top = (ImageButton)findViewById(R.id.topbutton);
top.setImageResource(R.drawable.yesbutton);
top.setVisibility(View.VISIBLE);
bottom = (ImageButton)findViewById(R.id.bottombutton);
bottom.setImageResource(R.drawable.nobutton);
bottom.setVisibility(View.VISIBLE);
}
}
它具有VideoView與setContentView(videoholder)有關的事情,這使得視頻佔據整個屏幕。相反,按鈕和視頻應該以某種方式傳遞給contentView。 – Cal 2012-04-09 03:01:57