我写了简单的网络视图应用程序在Android中,但进度条显示的是应用程序的顶部点击here查看图像。实际上,我想这样做点击here
这是我的主要活动
public class MainActivity extends AppCompatActivity implements MyWebChromeClient.ProgressListener {
WebView wv;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv=(WebView) findViewById(R.id.webview);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
settings.getLoadsImagesAutomatically();
settings.getBuiltInZoomControls();
settings.setSaveFormData(true);
settings.setEnableSmoothTransition(true);
settings.setNeedInitialFocus(true);
settings.setSavePassword(true);
settings.setDomStorageEnabled(true);
settings.setPluginState(WebSettings.PluginState.ON);
// add progress bar
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
wv.loadUrl("https://www.w3schools.com/");
wv.setWebChromeClient(new MyWebChromeClient(this));
wv.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
}
});
}
@Override
public void onBackPressed() {
if(wv.canGoBack()){
wv.goBack();
}
else{
super.onBackPressed();
}
}
@Override
public void onUpdateProgress(int progressValue) {
mProgressBar.setProgress(progressValue);
if (progressValue == 100) {
mProgressBar.setVisibility(View.INVISIBLE);
}
}}
在这个布局中我添加了webview和进度条。
- 活动_主要. 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.johnson.apple.MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ProgressBar
android:id="@+id/progressBar"
android:minHeight="2dip"
android:maxHeight="2dip"
android:progressDrawable="@drawable/bg_progress_bar_webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal" />
</RelativeLayout>
希望任何人都能帮助我Thnaks提前
3条答案
按热度按时间aydmsdu91#
添加到进度栏中
所以进度条应该是
bnl4lu3b2#
将
android:layout_centerInParent="true"
添加到进度栏布局像这样:
qaxu7uf23#
我对此做了一些改进,答案是修改activity_xml
现在起作用了