我是Android应用程序的新手,这是我的第一个应用程序。 我已经创建了启动画面并且可以正常工作..但是在它消失了一个长长的白色空白屏幕大约2-5秒后,网址开始加载..
我的问题是..如何在加载URL时在Splash Screen中制作一些加载或进度条?所以没有白色的空白屏幕。抱歉我的英语不好。
的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>
答案 0 :(得分:3)
不要将启动画面设置为单独的活动。在您的WebView
活动中添加启动功能,即您的网页加载活动。收听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);
}
}
});
答案 1 :(得分:1)
尝试这两个选项中的任何一个。它会帮助你
1.删除回调处理程序
handel.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent loadSplash = new Intent(Splash.this, MyActivity.class);
startActivity(loadSplash);
finish();
handel.removeCallbacks(this);
}
}, 3000);
使用线程代替处理程序
public class Splash extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000;
Thread splashTread;
private boolean stop = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
splashTread = new Thread()
{
@Override
public void run() {
try {
int waited = 0;
while (_active && (waited < _splashTime)) {
sleep(100);
if (_active) {
waited += 100;
}
}
} catch (InterruptedException e) {
// do nothing
} finally {
if (!stop) {
startActivity(new Intent(Splash.this, MyActivity.class));
finish();
} else
finish();
}
}
};
splashTread.start();
}
}
答案 2 :(得分:0)
以下是如何操作 -
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView1).setVisibility(View.VISIBLE);
}
});
wv.loadUrl("http://yoururlhere.com");
您可以在此处找到完整示例 - Splash Screen While Loading a URL
答案 3 :(得分:0)
您可以使用 AsyncTask &amp;将该加载URL代码放入AsyncTask&amp;使用进度条查看进度。
帮助链接:
我希望这对你有所帮助。
答案 4 :(得分:0)
在这里你去.. Jibran Khan说,不要创建单独的启动活动。在webview上使用启动功能。首先通过扩展为WebViewClient
创建一个webclient类,你将得到像这样的未实现的方法。
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onLoadResource(WebView view, String url) {
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar1.setVisibility(View.VISIBLE);
webview.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
progressBar1.setVisibility(View.GONE);
webview.setVisibility(View.VISIBLE);
}
}
` 使用进度条作为上面代码中的加载指示器。
请勿忘记执行此操作web.setWebViewClient(new MyWebViewClient());
其中MyWebViewClient
是您的webclient类名称
这里是上面代码的xml
<WebView
android:id="@+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />