使用WebView在Android中嵌套html網頁

對於一些html學習較好的朋友,開發Android項目的時候把html網頁嵌套手機中,寫起來比android佈局要方便很多,那麼下面就和大家分享一下怎麼把html頁面嵌套到android佈局中

頁面:

    重點使用的android控件爲WebView

  第一步:  在你的佈局中實例一個WebView,並且取一個id

   <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/wv_produce"
        >
    </WebView>
  第二步:  把你編寫的html文件和相應的文件夾,新建一個和res同級的文件夾assets,必須以assets命名

  第三步:  實例一個相對應的java文件

 super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_produce);
        wv_produce = (WebView) findViewById(R.id.wv_produce);
        //這裏的文件路徑是死定的,把html文件名改掉就可以了
        wv_produce.loadUrl("file:///android_asset/produce.html");
        //支持App內部JavaScript交互
        wv_produce.getSettings().setJavaScriptEnabled(true);
        //自適應手機屏幕
        wv_produce.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wv_produce.getSettings().setLoadWithOverviewMode(true);

        //設置是否出現縮放工具
        wv_produce.getSettings().setBuiltInZoomControls(true);
        //設置可以支持縮放
        wv_produce.getSettings().setSupportZoom(true);
        //擴大比例的縮放
        wv_produce.getSettings().setUseWideViewPort(true);

        //設置編碼爲utf-8
        wv_produce.getSettings().setDefaultTextEncodingName("utf-8");


對了,別忘記配置哈

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章