============问题描述============
我给WEBVIEW加了等待的圆圈,怎么不起作用?
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="70" android:layout_gravity="center" android:visibility="gone"/> </LinearLayout>
主程序代码:
package com.ruihuo.talk;
import com.ruihuo.talk.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private WebView webview;
private ProgressBar myProgressBar;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
myProgressBar = (ProgressBar)findViewById(R.id.progressBar);
myProgressBar.setIndeterminate(false);
webview=(WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
//handler.cancel(); // Android默认的处理方式
handler.proceed(); // 接受所有网站的证书
//handleMessage(Message msg); // 进行其他处理
}
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
super.onPageStarted(webview, url, favicon);
myProgressBar.setVisibility(View.VISIBLE);
}
//加载完成时要做的工作
public void onPageFinished(WebView webview, String url) {
super.onPageFinished(webview, url);
myProgressBar.setVisibility(View.GONE);
}
// 加载错误时要做的工作
public void onReceivedError(WebView webview, int errorCode,String description, String failingUrl) {
Toast.makeText(MainActivity.this,
errorCode+ "/" + description, Toast.LENGTH_LONG)
.show();
}
});
//去掉横向滚动条
webview.setHorizontalScrollBarEnabled(false);
//去掉垂直滚动条
webview.setVerticalScrollBarEnabled(false);
//去掉百边
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//设置WebView属性,能够执行JavaScript脚本
webview.getSettings().setJavaScriptEnabled(true);
//加载URL内容
webview.loadUrl("https://www.test.com/");
}
//设置回退
public boolean onKeyDown(int keyCode,KeyEvent event)
{
if((keyCode==KeyEvent.KEYCODE_BACK)&&webview.canGoBack())
{
webview.goBack();
return true;
}
return super.onKeyDown(keyCode,event);
}
//web视图客户端
public class MyWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
希望达到的效果是,启动程序后,出现圆圈转动,等待页面加载好,圆圈就消失.目前程序可以运行,单是看不到圆圈.求解决
也就是 myProgressBar.setVisibility(View.VISIBLE);
没起作用.
============解决方案1============
布局文件中,将ProgressBar的android:visibility="gone"改为android:visibility="invisible"
============解决方案2============
myProgressBar.setIndeterminate(false);这行代码做什么的?
mIndeterminate为true的情况,去setVisible()才能有动画。
============解决方案3============
把linearlayout改成relativelayout ,并设置progressbar Android:centerinparent="true"
============解决方案4============
难道你们没有发现是布局问题吗?
============解决方案5============

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="70" android:layout_centerInParent="true" android:visibility="visible"/> </RelativeLayout >
浙公网安备 33010602011771号