首先写布局文件activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/video_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" ></FrameLayout> <Button android:id="@+id/video_landport" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="全屏不显示该按扭,点击切换横屏" android:gravity="center" /> <WebView android:id="@+id/video_webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
原理:实现全屏的时候把webview里的视频放到一个View(布局里的video_view控件)里面,然后把webview隐藏掉!这样就实现了全屏播放的!
现在具体来看看怎么实现的:
先放代码MainActivity.java:
public class MainActivity extends Activity { private FrameLayout videoview;// 全屏时视频加载view private Button videolandport; private WebView videowebview; private Boolean islandport = true;//true表示此时是竖屏,false表示此时横屏。 private View xCustomView; private xWebChromeClient xwebchromeclient; private String url = "http://look.appjx.cn/mobile_api.php?mod=news&id=12604"; private WebChromeClient.CustomViewCallback xCustomViewCallback; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉应用标题 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); initwidget(); initListener(); videowebview.loadUrl(url); } private void initListener() { // TODO Auto-generated method stub videolandport.setOnClickListener(new Listener()); } private void initwidget() { // TODO Auto-generated method stub videoview = (FrameLayout) findViewById(R.id.video_view); videolandport = (Button) findViewById(R.id.video_landport); videowebview = (WebView) findViewById(R.id.video_webview); WebSettings ws = videowebview.getSettings(); /** * setAllowFileAccess 启用或禁止WebView访问文件数据 setBlockNetworkImage 是否显示网络图像 * setBuiltInZoomControls 设置是否支持缩放 setCacheMode 设置缓冲的模式 * setDefaultFontSize 设置默认的字体大小 setDefaultTextEncodingName 设置在解码时使用的默认编码 * setFixedFontFamily 设置固定使用的字体 setJavaSciptEnabled 设置是否支持java script * setLayoutAlgorithm 设置布局方式 setLightTouchEnabled 设置用鼠标激活被选项 * setSupportZoom 设置是否支持变焦 * */ ws.setBuiltInZoomControls(true);// 隐藏缩放按钮 ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);// 排版适应屏幕 ws.setUseWideViewPort(true);// 可任意比例缩放 ws.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。 ws.setSavePassword(true); ws.setSaveFormData(true);// 保存表单数据 ws.setjava scriptEnabled(true); ws.setGeoloc