Android在Webview中启用缩放和缩放控件

时间:2017-05-27 14:17:25

标签: android webview

我试图让缩放控件显示以及缩放以缩放以在Android webview中工作。主要参考this SO

现在我在onCreate方法中设置了这些:

this._webView.getSettings().setBuiltInZoomControls(true);
this._webView.getSettings().setDisplayZoomControls(true);
this._webView.getSettings().setSupportZoom(true);

但没有任何内容出现,缩放控件也不会出现。我在没有setSupportZoom的情况下也尝试过,但这似乎没什么帮助。

据我所知,我的webview没有包含在滚动视图中,但它是Webview的子类:

class MyWebView extends WebView {
        public MyWebView(Context context) {
            super(context);
        }

        @Override
        public void goBack() {

            super.goBack();
        }

    }

所以我最终创建它:

if (this._webView == null) {
            this._webView = new MyWebView(this);
            this._webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            this._webView.getSettings().setJavaScriptEnabled(true);
            this._webView.setWebChromeClient(new WebChromeClient());
            this._webView.setWebViewClient(new WebViewClient());
            this._webView.getSettings().setBuiltInZoomControls(true);
            this._webView.getSettings().setDisplayZoomControls(true);
            this._webView.getSettings().setSupportZoom(true);
            this._webView.loadUrl(this._baseUrl);
        }

我出错的任何想法?

谢谢!

编辑:

onCreate我这样做:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(1);
    getWindow().setFlags(1024, 1024);

    RelativeLayout my_layout = generateLayout();

    initUI();

    setContentView(my_layout);
}

在generateLayout中,我们创建初始布局并为webView创建持有者:

private RelativeLayout generateLayout() {
    RelativeLayout my_layout = new RelativeLayout(this);
    my_layout.setId(this.generateViewId());
    my_layout.setLayoutParams(new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT));

    ... more layout stuff

    FrameLayout webViewPlaceholder = new FrameLayout(this);
    this._webViewPlaceholder = webViewPlaceholder;
    webViewPlaceholder.setId(this.generateViewId());
    webViewPlaceholder.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
    RelativeLayout.LayoutParams webViewPlaceholderLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    webViewPlaceholderLayoutParams.addRule(RelativeLayout.BELOW, rlHeader.getId());
    webViewPlaceholder.setLayoutParams(webViewPlaceholderLayoutParams);

    my_layout.addView(webViewPlaceholder);
 }

然后在initUI我们创建了webView本身:

@SuppressLint({"SetJavaScriptEnabled"})
protected void initUI() {
    if (this._webView == null) {
        this._webView = new MyWebView(this);
        this._webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        this._webView.getSettings().setJavaScriptEnabled(true);
        this._webView.setWebChromeClient(new WebChromeClient());
        this._webView.setWebViewClient(new WebViewClient());

        this._webView.getSettings().setSupportZoom(true);
        this._webView.getSettings().setBuiltInZoomControls(true);
        this._webView.getSettings().setDisplayZoomControls(true);

        this._webView.loadUrl(this._baseUrl);

    } else {
        if (this._webView.getProgress() >= 100) {
            this._progressIndicatorView.setVisibility(View.INVISIBLE);
        }
        setNavigationButtonsState();
    }
    this._webViewPlaceholder.addView(this._webView);
}

3 个答案:

答案 0 :(得分:1)

这是我的代码,

public class PdfViewActivity extends AppCompatActivity {

            private Toolbar mToolbar;
            private TextView mTxtTitleToolbar;
            private WebView mWebViewPdf;
            private String strUrl;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_pdf_view);
                Intent intent = getIntent();
                strUrl = intent.getStringExtra("strUrl");
                setUI();
            }

            private void setUI() {
                mWebViewPdf = (WebView) findViewById(R.id.webview);
                mToolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(mToolbar);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);
                mTxtTitleToolbar = (TextView) findViewById(R.id.text_toolbar);
                mTxtTitleToolbar.setText("PDF");
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                loadWebView();
            }


            public void loadWebView() {
                mWebViewPdf.setWebViewClient(new myWebClient());
                mWebViewPdf.getSettings().setJavaScriptEnabled(true);
                mWebViewPdf.getSettings().setLoadWithOverviewMode(true);
                mWebViewPdf.getSettings().setUseWideViewPort(true);
                mWebViewPdf.getSettings().setBuiltInZoomControls(true);
                mWebViewPdf.getSettings().setDisplayZoomControls(false);
                mWebViewPdf.loadUrl("http://docs.google.com/gview?embedded=true&url="
+ strUrl.trim());
            }

            public class myWebClient extends WebViewClient {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                @Override
                public void onPageFini`enter code here`shed(WebView view, String url) {
                    super.onPageFinished(view, url);
                }
            }
        }

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:foo="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/rel_product_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/color_Black"
            app:contentInsetStart="@dimen/d_0"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/AppTheme.AppBarOverlay">

            <customview.CustomTextView
                android:id="@+id/text_toolbar"
                style="@style/TitleTextView"
                android:paddingRight="@dimen/d_40"
                foo:FontEnum="light" />

        </android.support.v7.widget.Toolbar>

    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/rel_product_detail"
        android:cacheColorHint="@color/transparent"
        android:fillViewport="true"
        android:scrollbars="none">

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

    </ScrollView> </RelativeLayout>

答案 1 :(得分:0)

在设置内容视图后调用您的initUI。您需要先获得对布局的引用,然后才能对其进行任何操作。如果要在generateLayout()中创建布局,则需要在处理Web视图设置之前进行设置。

答案 2 :(得分:-2)

尝试以下代码。

you can do something like this:

builder.setNeutralButton("Back", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Level1.this, LevelPage.class);
                        intent.putExtra("SCORE", score);
                        startActivity(intent);
                    }
                });

//In your level activity where you have btnlvl2
// int or String based on what have you decalred your score varaible
Intent i = getIntent();
String score = i.getStringExtra("SCORE");

if (score.equals(30)){
        btnLevel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickSound.start();
                    Intent intent = new Intent(LevelPage.this, Level2.class);
                    startActivity(intent);

            }
        });
    }