Ich versuche, eine Fortschrittsbalken oben auf der App wie Google Chrome für Android zeigt Ladebalken beim Laden einer Website zu zeigen.Zeigen Sie eine Fortschrittsbalken an der Spitze beim Laden von Webview App
Ich möchte diese Fortschrittsleiste anzeigen, wenn der Benutzer auf interne Links der Webview App klickt ... genau wie Google Chrome zeigt.
Hier ist, was ich versucht habe, funktioniert dieser Code nicht, App funktioniert nicht, auch wenn es nicht startet.
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(null);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.main_webview);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
if (mWebView.getProgress() == 100) {
// show webview
mWebView.setVisibility(View.VISIBLE);
// hide splash
findViewById(R.id.splash_screen).setVisibility(View.GONE);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
view.loadUrl("file:///android_asset/offline.html");
}
});
mWebView.loadUrl("http://www.example.com/?m=1");
}
}
Haupttätigkeit:
<?xml version="1.0" encoding="utf-8"?>
<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">
<ImageView
android:id="@+id/splash_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/splash_logo"
android:visibility="visible" />
<WebView
android:id="@+id/main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressBar"
android:minHeight="2dip"
android:maxHeight="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal" />
</RelativeLayout>
Bewegen Sie den 'findViewById()' Aufruf nach dem 'setContentView()' Anruf. Stellen Sie darüber hinaus einen 'WebChromeClient' auf der 'WebView' ein, um Aktualisierungen des Seitenfortschritts in der 'onProgressChanged()' Methode zu erhalten. –