【发布时间】:2013-01-21 22:59:15
【问题描述】:
我有一个 HTML5 网页,我想为 Android DSK 使用 webview。
这是我的 androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我在布局文件夹中的activitymain.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
我还应该做什么?以及将该代码放在哪里?
Google android 文档是这样说的:
要在 WebView 中加载网页,请使用 loadUrl()。例如:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");
但是我应该把这些代码行放在哪里呢?在新文件中?我尝试了很多事情都没有成功。
【问题讨论】:
-
只需将两行 WebView 代码放在活动的 onCreate() 方法中,在 setContentView(...) 下方。
-
@Adrian Diaz 查看发布的解决方案以解决您的问题。
-
abhi ,好的,你能发布一个示例代码吗?我可以把它放在哪里。