2017-05-27 53 views
7

我想要獲取縮放控件以顯示與捏縮放以在android webview中工作。大量參考this SOAndroid在Web視圖中啓用縮放和捏控制

現在我已經被這些在onCreate方法設置:

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

但沒有出現,縮放控件沒有出現。我也試過沒有setSupportZoom但這似乎沒有幫助。

至於我可以告訴我的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我們創造的初始佈局和web視圖創建持有者:

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); 
} 
+0

我認爲你沒有得到對web視圖的引用。嘗試使用webview作爲佈局視圖,而不是動態創建 –

+0

您是否將'this._webView'添加到佈局中? RootView應該將webview添加到其佈局中。 – cole

+0

我向主'RelativeLayout'添加一個'FrameLayout',然後添加'this._webView'。主佈局以編程方式創建。你想看代碼嗎? –

回答

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> 
0

請嘗試以下代碼。

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

打電話給你initUI設置後的內容視圖。您需要先獲得對佈局的參考,然後才能對其進行任何操作。您正在使用generateLayout()創建佈局,您需要在處理Web視圖設置之前對其進行設置。