android - 添加 admob 横幅广告时 WebView 不显示
问题描述
我不知道为什么当我放 admob 横幅广告时我的 webView 是 show show
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
当我删除 admob 时,我的 webview 显示完美
web.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="5dp">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/myProcessBar"
android:layout_weight="0.1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="100"
android:progress="45"
/>
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_below="@+id/pb"
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="fill_parent"
android:layout_height="fill_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
请有人帮助我:-
web_main.java
package com.musicwala.djaman;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import org.apache.http.conn.util.PublicSuffixList;
import android.webkit.DownloadListener;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.app.Dialog;
import android.widget.Toast;
import android.os.Build;
import android.content.pm.PackageManager;
import android.app.DownloadManager;
import android.net.Uri;
import android.webkit.URLUtil;
import android.support.v4.app.ActivityCompat;
import android.Manifest;
import android.view.Menu;
import android.view.MenuInflater;
import java.util.zip.Inflater;
import android.view.MenuItem;
import android.content.Intent;
import android.text.style.UpdateAppearance;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class web_main extends Activity
{
private AdView mAdView;
ProgressBar superProgressBar;
WebView superWebView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// TODO: Implement this method
setContentView(R.layout.web);
MobileAds.initialize(this, "ca-app-pub-1075872442233861~4332615881");
superProgressBar = (ProgressBar) findViewById(R.id.myProcessBar);
superWebView = (WebView) findViewById(R.id.webview);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//Set Max Progress Bar
superProgressBar.setMax(100);
superWebView.loadUrl("http://musicwala.cf");
superWebView.getSettings().setJavaScriptEnabled(true);
final SwipeRefreshLayout finalMySwipeRefreshLayout1;
finalMySwipeRefreshLayout1 = findViewById(R.id.swiperefresh);
finalMySwipeRefreshLayout1.setColorSchemeColors(Color.BLUE, Color.YELLOW, Color.GREEN);
finalMySwipeRefreshLayout1.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
superWebView.loadUrl(superWebView.getUrl());
//finalMySwipeRefreshLayout1.setVisibility(View.INVISIBLE);
}
});
superWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
finalMySwipeRefreshLayout1.setRefreshing(false);
//.setVisibility(View.GONE);
}
});
superWebView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view,int newprogress){
super.onProgressChanged(view, newprogress);
superProgressBar.setProgress(newprogress);
superProgressBar.setProgressTintList(ColorStateList.valueOf(Color.GREEN));
if(newprogress == 100)
{
superProgressBar.setVisibility(view.GONE);
}
else
{
superProgressBar.setVisibility(view.VISIBLE);
}
}
@Override
public void onReceivedTitle(WebView view,String title){
super.onReceivedTitle(view, title);
getActionBar().setTitle(title);
getActionBar().setSubtitle("MusicWala");
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));
}
});
superWebView.setDownloadListener(new DownloadListener(){
@Override
public void onDownloadStart(String url, String useragent, String contentdisposition, String mimetype, long contentlenth)
{
// TODO: Implement this method
alertbox(url,useragent,contentdisposition,mimetype);
}
private void alertbox(final String url,final String useragent,final String contentdispositon,final String mimetype)
{
// TODO: Implement this method
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(web_main.this);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setTitle("Download");
alertDialogBuilder.setMessage("Do You Want To Download");
alertDialogBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface Dialog,int which){
//Download Start
//Checking Android Version
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
//Androidb Version 6 or upper
{
if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
//Now Downlaod Start * Permission is granded
downloadStart(url,useragent,contentdispositon,mimetype);
}
else
{
//Permission request
ActivityCompat.requestPermissions(web_main.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Toast.makeText(web_main.this,"Click On Download Button",Toast.LENGTH_SHORT).show();
}
}
else
{
//Android Version 5 or below
downloadStart(url,useragent,contentdispositon,mimetype);
}
}
private void downloadStart(String url, String useragent, String contentdispositon, String mimetype)
{
// TODO: Implement this method
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
final String fileName=URLUtil.guessFileName(url,contentdispositon,mimetype);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir("/MusicWala",fileName);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading Started...", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface Dialog,int which){
//Download Cancel
Dialog.dismiss();
}
});
alertDialogBuilder.create().show();
}
});
}
@Override
public void onBackPressed()
{
// TODO: Implement this method
//super.onBackPressed();
if(superWebView.canGoBack()){
superWebView.goBack();
}
else
{
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
MenuInflater Inflater = getMenuInflater();
Inflater.inflate(R.menu.all_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO: Implement this method
switch (item.getItemId())
{
case R.id.download:
//Settings Item
Intent download= new Intent(web_main.this,download.class);
startActivity(download);
break;
case R.id.settings:
//Settings Item
Intent setting= new Intent(web_main.this,setting.class);
startActivity(setting);
break;
case R.id.update:
//Update Item
Intent Update = new Intent(web_main.this,update.class);
startActivity(Update);
break;
case R.id.about:
//Settings Item
Intent about = new Intent(web_main.this,about.class);
startActivity(about);
break;
case R.id.exit:
//Settings Item
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
break;
}
return super.onOptionsItemSelected(item);
}
}
解决方案
您的代码看起来非常干净和正确,让我们尝试更改第一个布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
至
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
看看会发生什么
还有你是什么意思
当我删除 admob
您不是在请求数据(例如通过注释掉)还是从 XMLmAdView.loadAd(adRequest);
中删除整个布局?<com.google.android.gms.ads.AdView
顺便提一句。删除您的唯一密钥和 ID,永远不要将它们发布到任何地方......
推荐阅读
- c - 在C中递归打印两个空心镜像三角形
- php - 如何将两个页面添加在一起
- python - 将多个文件合并到一个文件夹中
- javascript - Next.js 表单在本地工作,但不能在实时服务器上工作
- python - Mypy:Tensorflow TFRecords `Bytes` 对象的 `mypy` 类型
- reactjs - Aframe 状态组件 - 模板在 Aframe 之外添加元素
- python - 合并拆分数组的剩余部分
- javascript - 计算没有 `.reduce()`、`.filter()` `forEach()` 的真/假数
- jsdoc - JSDoc:根据另一个参数使参数成为必需/可选
- elasticsearch - ElasticSearch 衰减最小值