如何在Android中设置WebView渲染优先级?
此示例演示如何在Android中设置WebView渲染优先级。
步骤1 − 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。
步骤2 − 将以下代码添加到res/layout/activity_main.xml。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:gravity = "center" android:layout_height = "match_parent" tools:context = ".MainActivity" android:orientation = "vertical"> <WebView android:id = "@+id/web_view" android:layout_width = "match_parent" android:layout_height = "match_parent" /> </LinearLayout>
在上面的代码中,我们使用了WebView来显示tutorialspoint.com。
步骤3 − 将以下代码添加到src/MainActivity.java
package com.example.myapplication; import android.app.ProgressDialog; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.EditText; public class MainActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.P) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setMessage("Loading Data..."); progressDialog.setCancelable(false); WebView web_view = findViewById(R.id.web_view); web_view.requestFocus(); web_view.getSettings().setLightTouchEnabled(true); web_view.getSettings().setJavaScriptEnabled(true); web_view.getSettings().setGeolocationEnabled(true); web_view.setSoundEffectsEnabled(true); web_view.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); web_view.getSettings().setUseWideViewPort(true); web_view.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); web_view.loadUrl("https://tutorialspoint.com/"); web_view.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { if (progress < 100) { progressDialog.show(); } if (progress = = 100) { progressDialog.dismiss(); } } }); } }
步骤4 − 将以下代码添加到AndroidManifest.xml
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.myapplication"> <uses-permission android:name = "android.permission.INTERNET"/> <application android:allowBackup = "true" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true" android:theme = "@style/AppTheme"> <activity android:name = ".MainActivity"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开您的项目中的一个活动文件,然后单击运行 工具栏中的图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 –
点击 这里 下载项目代码
广告