【问题标题】:Set webview to full screen among other controllers在其他控制器中将 webview 设置为全屏
【发布时间】:2013-12-18 15:30:39
【问题描述】:

我有一个带有几个按钮和 web 视图的活动。我想在按下特定按钮时将 webview 设置为全屏,并在双击 webview 时退出全屏模式。 这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageButton
                android:id="@+id/ibNext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_action_previous_item" />
            <ImageButton
                android:id="@+id/ibPrevious"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_action_next_item" />

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right" >

                <ImageButton
                    android:id="@+id/ibFullScreen"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_action_full_screen" />

                <ImageButton
                    android:id="@+id/ibChapters"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_action_view_as_list" />


            </LinearLayout>


        </LinearLayout>

        <WebView
            android:id="@+id/webView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>

我已经有一个全屏按钮的监听器,我只需要知道如何将 webview 设置为全屏。

谢谢

【问题讨论】:

    标签: android webview fullscreen


    【解决方案1】:

    这样做

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <LinearLayout
                android:id="@+id/lnrOptions"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <ImageButton
                    android:id="@+id/ibNext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
    
                <ImageButton
                    android:id="@+id/ibPrevious"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
    
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:orientation="horizontal" >
    
                    <ImageButton
                        android:id="@+id/ibFullScreen"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" />
    
                    <ImageButton
                        android:id="@+id/ibChapters"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" />
                </LinearLayout>
            </LinearLayout>
    
            <WebView
                android:id="@+id/webView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
    
    </LinearLayout>
    

    在您的 Java 代码中

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnDoubleTapListener;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.ImageButton;
    import android.widget.LinearLayout;
    
    public class MainActivity extends Activity implements OnGestureListener {
    
        private WebView webView1;
        private ImageButton ibFullScreen;
        private LinearLayout lnrOptions;
        GestureDetector gd;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            webView1 = (WebView) findViewById(R.id.webView1);
            webView1.setWebViewClient(new WebViewClient());
            webView1.loadUrl("http://www.google.com");
            ibFullScreen = (ImageButton) findViewById(R.id.ibFullScreen);
            lnrOptions = (LinearLayout) findViewById(R.id.lnrOptions);
    
            gd = new GestureDetector(this);
            gd.setOnDoubleTapListener(new OnDoubleTapListener() {
    
                @Override
                public boolean onSingleTapConfirmed(MotionEvent e) {
                    return false;
                }
    
                @Override
                public boolean onDoubleTapEvent(MotionEvent e) {
                    return false;
                }
                //UPDATE HERE
                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    lnrOptions.setVisibility(View.VISIBLE);
                                getActionBar().show();
                    return true;
                }
            });
    
            webView1.setOnTouchListener(new OnTouchListener() {
    
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    return gd.onTouchEvent(arg1);
                }
            });
                //UPDATE HERE
            ibFullScreen.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    if (lnrOptions.getVisibility() == View.VISIBLE) {
                        lnrOptions.setVisibility(View.GONE);
                                        getActionBar().hide();
                    } else {
                        lnrOptions.setVisibility(View.VISIBLE);
                    }
                }
            });
    
        }
    
        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    
        @Override
        public void onLongPress(MotionEvent e) {
    
        }
    
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }
    
        @Override
        public void onShowPress(MotionEvent e) {
    
        }
    
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }
    
    }
    

    【讨论】:

    • 感谢您的回答。我使用了您的答案,现在全屏按钮将 webview 传播到按钮区域,但 ActionBar 仍然存在。如何将 webview 也传播到 ActionBar 的区域?
    • 我更新了我的答案,试试它现在可以根据您的需要工作。
    • 再次感谢您,但您对 ActionBar 的更新适用于 API 级别 11 及更高级别。我需要它用于 API 级别 7。你知道在 API 级别 7 中执行此操作的方法是什么吗?
    • 您应该使用“android-support-v13.jar”来实现向后兼容性。并使用“getSupportActionBar().hide();getSupportActionBar().show();”而是 getActionBar()。
    • @rafraph 很高兴知道它的工作原理。请接受答案,以便其他人获得帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2014-03-16
    • 1970-01-01
    相关资源
    最近更新 更多