我正在开发一个包含Webview的应用程序。我将RelativeLayout
作为xml文件的根元素。 RelativeLayout
包含ProgressBar
,SwipeRefreshLayout
,WebView
和AdView
元素。默认情况下,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>
我尝试将LinearLayout
与weight
和weightsum
属性一起使用,然后显示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>
请帮我解决这个问题。我同时需要ProgressBar
和AdView
。
我正在使用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);
}
});
答案 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);
}