我已经完成了使用ActionBarSherlock向我的WebView添加ProgressBar的所有必要步骤,但我没有在Android 4.0之前获得任何结果。有人可以帮忙吗?我从ABS导入了Window,添加了requestWindowFeature(Window.FEATURE_PROGRESS);在设置内容之前,我只是不知道该怎么做。请不要告诉我按照样品,因为我已经尝试过,但我没有得到任何结果。谢谢。
以下是MainActivity.class的代码
package com.twk95.webviewtest;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
public class MainActivity extends SherlockActivity {
public static WebView webView;
final SherlockActivity MyActivity = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
webview.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.google.com");
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100);
setSupportProgressBarVisibility(true);
if (progress == 100) {
MyActivity.setTitle(R.string.app_name);
setSupportProgressBarVisibility(false);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.webview_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.webReload:
webView.reload();
break;
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return false;
}
}
答案 0 :(得分:1)
确保requestWindowFeature
方法正在使用com.actionbarsherlock.app.SherlockActivity.requestWindowFeature(long featureId)
,然后使用setSupportProgress(int progress)
进行更新,而不是使用MyActivity.setProgress()