gpt4 book ai didi

java - Android 网页 View : Splash screen while loading a url

转载 作者:行者123 更新时间:2023-12-01 18:24:38 25 4
gpt4 key购买 nike

我是 Android 应用程序的新手,这是我的第一个应用程序。我已经创建了启动屏幕并且可以工作..但是在它消失后,会出现一个长长的白色空白屏幕,大约 2-5 秒,然后 url 开始加载..

我的问题是..如何在加载 URL 时在启动屏幕中制作一些加载或进度条?所以不再有白色的空白屏幕。抱歉我的英语不好。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.xxx" >

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


<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="xxx"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true" />


<activity
android:name="com.xxx.xxx.Splash"
android:label="xxx"
android:theme="@style/Theme.MyAppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyActivity"
android:hardwareAccelerated="true"
android:configChanges="orientation|screenSize"
android:label="xxx" >

</activity>

</application>

</manifest>

activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MyActivity">

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

<TextView
android:id="@+id/display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>

<FrameLayout
android:id="@+id/customViewContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"/>
</LinearLayout>

Splash.java

    import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);

final Handler handel = new Handler();
handel.postDelayed(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
Intent loadSplash = new Intent(Splash.this, MyActivity.class);

startActivity(loadSplash);

finish();
}
}, 3000);
}
}

splashscreen.xml

<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >

<ImageView
android:id="@+id/imageView1"

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/splash" />

</RelativeLayout>

最佳答案

不要将启动屏幕创建为单独的 Activity 。在您的 WebView Activity (即页面加载 Activity )上添加启动功能。监听 onPageFinished 事件并隐藏启动图像或动画。

mWebView.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url) {
//hide splash functionality
}
});

如果您想显示进度条,您还可以获取页面加载进度

mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if(progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
}
}
});

关于java - Android 网页 View : Splash screen while loading a url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26582457/

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