我正在创建一个使用多个本地 html 文件(都在资产目录中)的应用程序。当我在文件 foo.html 中创建一个锚点并在 bar.html 中创建指向该锚点的链接,然后在我的 Galaxy Nexus 上查看它时,我的 webview 说该网页不可用。
我相当确定 html 已正确完成。w3c HTML5 验证器通过了它,当我在我的计算机上查看网页时,导航可以正常工作,并且当使用运行 2.2.3 而不是 Nexus 的摩托罗拉机器人时,该应用程序可以正常工作。
当锚点和指向它的链接在同一个 html 文件中时,锚点起作用,并且当链接中不包含锚点时,页面加载得很好(当然,不是在我希望它们所在的位置)。
如果有人对此有解决方案或有不同的方法,我会很高兴听到。
在我的研究中,我发现 Android 操作系统中存在一个类似问题的错误,但它被标记为已发布,所以我怀疑是它。 http://code.google.com/p/android/issues/detail?id=17535
这里有一个类似的问题,Android Webview Anchor Link (Jump link) not working,但我没有任何滚动视图。
这是我唯一的活动:
package com.me.Foo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
//import android.webkit.WebViewClient;
import android.widget.Button;
public class Foo extends Activity implements OnClickListener {
WebView browser;
public static final String RDR = "RDR";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.backButton)).setOnClickListener(this);
((Button) findViewById(R.id.forwardButton)).setOnClickListener(this);
browser = (WebView) findViewById(R.id.webView);
//browser.loadUrl("file:///android_asset/table_of_contents.html");
browser.loadUrl("file:///android_asset/game_parameters.html#2.9");
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.backButton:
Log.d(RDR, "Back button pressed.");
if(browser.canGoBack())
browser.goBack();
break;
case R.id.forwardButton:
Log.d(RDR, "Forward button pressed.");
if(browser.canGoForward())
browser.goForward();
break;
default:
Log.d(RDR, "ERROR! That button click is not handled.");
}
}
}
这是我的 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/naviagtionLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/backButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="@string/backward" />
<Button
android:id="@+id/forwardButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="@string/forward" />
</LinearLayout>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@id/naviagtionLayout" />
</RelativeLayout>
谢谢。