有时候需要在应用程序里展示一些网页,但是需求里又明确指出,不允许打开系统浏览器,显然也不可能去编写一个浏览器出来,这时就需要使用 WebView控件,借助它我们就可以在自己的应用程序里嵌入一个浏览器,从而非常轻松地展示各种各样的网页。

由于程序用到了网络功能,而访问网络是需要声明权限的,因此首先得修改 AndroidManifest.xml 文件,并加入声明权限:

<uses-permission android:name="android.permission.INTERNET" />

然后修改 activity_main.xml 中的代码,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
activity_main.xml

相关文章: