在其他控制器中将webview设置为全屏

时间:2013-12-17 07:07:41

标签: android webview fullscreen

我有一个带有几个按钮和webview的活动。我想在按下特定按钮时将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设置为全屏。

由于

1 个答案:

答案 0 :(得分: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;
    }

}