webView zoomin zoomout 不好使

zhoujiaolongde 2011-12-16 11:01:16
为什么 我将一个网上的例子下下来运行不好使呢?我想实现点击按钮放大和缩小webview的页面这个功能。求帮助谢谢。

这是我的activity
package com.hundsun.zhoujl.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TestZoomActivity extends Activity
implements OnClickListener, OnKeyListener {
Button zoomin;
Button zoomout;
Button info_title;
Button info_url;
EditText url;
WebView webView;
//Example04程序
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example04);
//建立显示画面上Button,EditText,WebView类别的实例变量
zoomin = (Button)findViewById(R.id.Button01);
zoomout = (Button)findViewById(R.id.Button02);
info_title = (Button)findViewById(R.id.Button03);
info_url = (Button)findViewById(R.id.Button04);
url = (EditText)findViewById(R.id.EditText01);
webView = (WebView)findViewById(R.id.WebView01);
webView.setWebViewClient(new WebViewClient());
//设定Button和EditText的监听功能
zoomin.setOnClickListener(this);
zoomout.setOnClickListener(this);
info_title.setOnClickListener(this);
info_url.setOnClickListener(this);
url.setOnKeyListener(this);
}
//按下Button时的处理程序
public void onClick(View v) {
if (v == zoomin) {
boolean ret = webView.zoomIn();
Toast.makeText(this, "zoom in is "+ret, Toast.LENGTH_SHORT).show();
} else if (v == zoomout) {
boolean ret = webView.zoomOut();
Toast.makeText(this, "zoom Out is "+ret, Toast.LENGTH_SHORT).show();
} else if (v == info_title) {
String title = webView.getTitle();
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage(title)
.setPositiveButton("Ok", null)
.show();
} else if (v == info_url) {
String url = webView.getUrl();
new AlertDialog.Builder(this)
.setTitle("URL")
.setMessage(url)
.setPositiveButton("Ok", null)
.show();
}
}
//於url(EditText)编辑框按下ENTER时的处理程序,下载載新的网页
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
webView.loadUrl(url.getText().toString());
return true;
}
return false;
}
}

xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="放大" />
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="缩小" />

<Button android:id="@+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="标题信息"></Button>
<Button android:id="@+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="链接信息"></Button>




</LinearLayout>
<EditText android:id="@+id/EditText01" android:layout_height="wrap_content"
android:text="http://" android:layout_width="fill_parent"
android:maxLines="1" />
<WebView android:id="@+id/WebView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


我是在模拟器上运行的 版本android2.2
...全文
273 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoujiaolongde 2011-12-19
  • 打赏
  • 举报
回复
最后总结一下 webView的zoomin和zoomout 的两个功能
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
这两句话之外 还需要注意你的测试网址 最好是google 因为有的网页不支持啥原因我也不清楚
还有就是用多点触控实现放大缩小除了上边两句话 还应该注意你的布局问题 要不然的话网页就会产生异常
zhoujiaolongde 2011-12-16
  • 打赏
  • 举报
回复
好使了 这玩意原来跟代码没关系 是跟访问的网页有关系 百度不行 but google给力了 谢谢raphae155兄的帮助 。结账。
raphael55 2011-12-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhoujiaolongde 的回复:]

引用 1 楼 raphael55 的回复:

webview.getSettings().setSupportZoom(true);
就可以了

谢谢你的回答 我试了一下还是不好使 不知道你试过好使没有?
[/Quote]
那再加上这句试试
webview.getSettings().setBuiltInZoomControls(true);

zhoujiaolongde 2011-12-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 raphael55 的回复:]

webview.getSettings().setSupportZoom(true);
就可以了
[/Quote]
谢谢你的回答 我试了一下还是不好使 不知道你试过好使没有?
raphael55 2011-12-16
  • 打赏
  • 举报
回复
webview.getSettings().setSupportZoom(true);
就可以了

80,352

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧