在Android中的同一屏幕上显示视频和按钮

时间:2012-04-07 18:12:15

标签: android layout view imagebutton android-videoview

我正在尝试在同一个屏幕上创建一个视频播放和两个ImageButton的视图。我正在使用VideoView来包含我的两个按钮的视频和ImageButtons。当我独立测试每个功能时,两者都能正确显示,但当我尝试在同一个屏幕上同时显示时,它们不会同时显示!我已经尝试了许多布局(帧,线性,相对),将VideoView限制为更小的layout_width& layout_height,并在xml文件中尝试过权重,但似乎没有任何工作。显示这个似乎太简单了,不需要自定义视图,但如果必须,我会这样做。

以下是我的问题:您知道如何在同一屏幕上显示imageButtons和VideoView吗? 创建自定义视图时,您可以使用VideoView和ImageButton等Android视图吗?或者你可以在自定义视图中的画布上绘制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);
}
}

1 个答案:

答案 0 :(得分:0)

我不是在一台可以测试它的电脑,但你可能想要尝试的是绝对布局。

您应该可以将按钮放在视频顶部,这是一个示例 http://www.tutorialforandroid.com/2009/01/absolutelayout-in-android-xml.html

您可能还想尝试将按钮和VideoView放在相同的布局中