gpt4 book ai didi

java - Android 中 Webview 之前避免白屏

转载 作者:行者123 更新时间:2023-12-02 04:07:19 24 4
gpt4 key购买 nike

我正在开发一个Android应用程序,我有两个 Activity splash和第一个。我正在将 web url 封装在 webview 中。但是,每当我单击 WebView 中的任何链接时,都会出现白屏。我怎样才能删除白屏,因为它给最终用户带来了非常烦人的体验。下面是我的代码

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.novjan">
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".first"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">

</activity>
<activity
android:name=".splash"
android:label="@string/title_activity_splash"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>
</application>

</manifest>

Splash.java

package com.example.admin.novjan;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class splash extends AppCompatActivity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(splash.this, first.class);
startActivity(i);

// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}

}

第一个.java

package com.example.admin.novjan;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class first extends AppCompatActivity {


private WebView wv1;


@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
// set content view
setContentView(R.layout.activity_first);
//set web view
wv1=(WebView)findViewById(R.id.webView);

//wv1.setWebViewClient(new MyWebViewClient());
wv1.setWebViewClient(new MyWebViewClient() {

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
wv1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

@Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progressBar1).setVisibility(View.GONE);

findViewById(R.id.webView).setVisibility(View.VISIBLE);


}
});
String url = "http://novjan.com/mhome/";
wv1.getSettings().setJavaScriptEnabled(true);
wv1.loadUrl(url);
}

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}//end of first class

content_first.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:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.admin.novjan.first"
tools:showIn="@layout/activity_first">

<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
/>

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
android:layout_centerInParent="true" />
</LinearLayout>

最佳答案

显示进度条:

public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (m_webView.getProgress() == 100) {
progressBar.setVisibility(View.GONE);
m_webView.setVisibility(View.VISIBLE);
}
}

通过 onCreate 或您认为可行的任何其他方法显示并启动进度。

关于java - Android 中 Webview 之前避免白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34147485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com