我看到了很多进度条,但是我无法将其应用于我的应用程序,请帮助我制作一个好的进度条。进度条应一直显示,直到片段中的网页加载到webView中为止。请根据我的代码创建代码,对不起,我是android新手,请帮助新手
片段代码
public class BlankFragment2 extends Fragment {
public Handler h;
TextView txt;
Bundle webViewBundle;
public AdView adView;
public WebView mWebView;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
public BlankFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view
= inflater.inflate(R.layout.blank_fragment2, container, false);
txt=(TextView)view.findViewById(R.id.hi);
Runnable r=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Log.e("bf", "fd");
h.postDelayed(this, 300);
if (txt.isShown()){
txt.setVisibility(View.INVISIBLE);
}
else{
txt.setVisibility(View.VISIBLE);
}
}
};
mWebView = (WebView) view.findViewById(R.id.hu);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setDisplayZoomControls(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setVerticalScrollBarEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.setHorizontalScrollBarEnabled(true);
this.mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mWebView.loadUrl("https://www.brainyquote.com/topics/love");
AdRequest adRequest = new AdRequest.Builder().build();
adView = (AdView) view.findViewById(R.id.ad9View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad10View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad13View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad14View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad11View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.ad12View);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn1);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn2);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn3);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn4);
adView.loadAd(adRequest);
adView = (AdView) view.findViewById(R.id.earn5);
adView.loadAd(adRequest);
mWebView.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return false;
}
});
return view;
}
@Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
mWebView.saveState(webViewBundle);
}
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".BlankFragment2">
<RelativeLayout android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tr" android:layout_width="match_parent" android:layout_height="match_parent">
<WebView android:id="@+id/hu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad9View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hi"
android:layout_centerHorizontal="true"
app:adSize="BANNER" app:adUnitId="@string/ad9View" />
<com.google.android.gms.ads.AdView
android:id="@+id/ad10View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad9View"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
app:adSize="BANNER"
app:adUnitId="@string/ad10View" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad11View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad10View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/ad11View" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad12View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad11View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/ad12View" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/earn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad14View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/earn" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/earn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/earn1"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/earn2" />
<com.google.android.gms.ads.AdView
android:id="@+id/earn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/earn2"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
app:adSize="BANNER"
app:adUnitId="@string/earn3" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/earn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/earn3"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/earn4" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/earn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/earn4"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/earn5" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad13View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad12View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/ad10View" />
<com.google.android.gms.ads.AdView
android:orientation="horizontal"
android:id="@+id/ad14View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad13View"
app:adSize="BANNER"
android:layout_centerHorizontal="true"
app:adUnitId="@string/ad9View" />
<TextView
android:id="@+id/hi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:textSize="20sp"
android:layout_below="@id/hu"
android:text="@string/text" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
答案 0 :(得分:0)
您正在寻找的是ProgressBar
您将ProgressBar视图放在XML布局中,并根据需要调用progressbar.setVisibility(GONE)或VISIBLE
答案 1 :(得分:0)
步骤1:在ProgressBar
下方添加到xml
布局中。
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
步骤2::初始化ProgressBar
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
步骤3:为您的WebView
设置客户端并按如下所示加载URL
mWebView.setWebViewClient(new MyBrowser());
mWebView.setWebChromeClient(new MyWebViewClient());
mWebView.loadUrl(your_url);
步骤4:如下设置进度。当进度为100%时将其隐藏。
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
private class MyWebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
WebViewActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
if (newProgress == 100){
progress.setVisibility(View.GONE);
}
else {
progress.setVisibility(View.VISIBLE);
}
}
}
答案 2 :(得分:0)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.your fragment name, container, false);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setMax(100);
progressBar.setProgress(0 );
然后
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE); }
最后
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE); }