进度栏未显示在Webview的Relativelayout中

时间:2018-06-23 09:28:38

标签: java android webview android-progressbar

我正在开发一个包含Webview的应用程序。我将RelativeLayout作为xml文件的根元素。 RelativeLayout包含ProgressBarSwipeRefreshLayoutWebViewAdView元素。默认情况下,ProgressBar的可见性设置为gone,它在加载网页时应显示,并且应再次不可见。但是,它没有显示任何时间。这是我的activity_web_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.softglobe.allmedia.Showsite"
    android:orientation="vertical">

<ProgressBar
    android:id="@+id/pb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progressDrawable="@drawable/progressbar_states"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:visibility="gone"/>


<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:configChanges="orientation|screenSize"/>

</android.support.v4.widget.SwipeRefreshLayout>

        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            android:layout_alignBottom="@id/swiperefresh"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>

我尝试将LinearLayoutweightweightsum属性一起使用,然后显示ProgressBar,但随后消失了AdView。使用LinearLayout的代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.softglobe.allmedia.Showsite"
    android:orientation="vertical"
    android:weightSum="20">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" >

<ProgressBar
    android:id="@+id/pb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progressDrawable="@drawable/progressbar_states"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:visibility="gone"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="18"
        android:orientation="horizontal" >

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:configChanges="orientation|screenSize"/>

</android.support.v4.widget.SwipeRefreshLayout>
    </LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2" >

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    android:layout_alignBottom="@id/swiperefresh"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</LinearLayout>

请帮我解决这个问题。我同时需要ProgressBarAdView。 我正在使用ProgressBar方法在Siteview.java文件中显示onCreate。以下是代码

 final SwipeRefreshLayout finalMySwipeRefreshLayout1 = mySwipeRefreshLayout;
    myWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
            // Visible the progressbar
            mProgressBar.setVisibility(View.VISIBLE);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            finalMySwipeRefreshLayout1.setRefreshing(false);
        }
    });

2 个答案:

答案 0 :(得分:2)

您的SwipeRefreshLayout位于ProgressBar的顶部,因此,即使使您的ProgressBar可见,它也会显示在SwipeRefreshLayout下,您将无法看到它,您可以使用

 `android:layout_below="@+id/pb"`

在您的SwipeRefreshLayout中,或者您可以将SwipeRefreshLayout的可见性设置为不可见,而进度条是可见的,而SwipeRefreshLayout的可见性则为ProgressBar。

答案 1 :(得分:0)

如下重写这两个功能

 @Override
            public void onPageStarted(String url, Bitmap favicon) {
                pb.setVisibility(View.VISIBLE);

            }

            @Override
            public void onPageFinished(String url) {
                pb.setVisibility(View.GONE);

            }