我想要獲取縮放控件以顯示與捏縮放以在android webview中工作。大量參考this SO。Android在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);
}
我認爲你沒有得到對web視圖的引用。嘗試使用webview作爲佈局視圖,而不是動態創建 –
您是否將'this._webView'添加到佈局中? RootView應該將webview添加到其佈局中。 – cole
我向主'RelativeLayout'添加一個'FrameLayout',然後添加'this._webView'。主佈局以編程方式創建。你想看代碼嗎? –