1. 程式人生 > >安卓設定videoview全屏

安卓設定videoview全屏

之前做一個播放器。videoview一直沒法全屏。真的是搜了一些資料什麼的。後來在一個網站上看到大神的解答。試了一下果然是有效的。擇出部分關鍵程式碼。

boolean fullscreen = false;
if(!fullscreen){//設定RelativeLayout的全屏模式
RelativeLayout.LayoutParams layoutParams=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
mVideoView.setLayoutParams(layoutParams);

fullscreen = true;//改變全屏/視窗的標記
}else{//設定RelativeLayout的視窗模式
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(300,400);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
mVideoView.setLayoutParams(lp);
fullscreen = false;//改變全屏/視窗的標記
}

佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.23" >
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>