Android 将AdBlock添加到布局中

Android 将AdBlock添加到布局中,android,admob,markup,Android,Admob,Markup,我是android开发的新手,所以向你寻求帮助 我有这样的代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_par

我是android开发的新手,所以向你寻求帮助

我有这样的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<LinearLayout android:layout_width="fill_parent"
    android:id="@+id/home_layout"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_above="@+id/adView">
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:id="@+id/progressBar" />
</LinearLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

</LinearLayout>

请在PddViewActivity.java中尝试:

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

private WebView webView;
private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pdd_view);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //instaciate AdView
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

progressBar = (ProgressBar) findViewById(R.id.progressBar);

String file = getIntent().getStringExtra("file");

webView = (WebView) findViewById(R.id.webview);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);


webView.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        Log.i("PROGRESS IS :", newProgress+"");
        progressBar.setProgress(newProgress);
    }
});

webView.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(getApplicationContext(), "Error: " + description + " " + failingUrl, Toast.LENGTH_LONG).show();
    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    public void onPageFinished(WebView view, String url) {
        if (progressBar.getVisibility() == WebView.VISIBLE) {
            progressBar.setVisibility(WebView.GONE);
        }
    }
});

webView.loadUrl(String.format("file:///android_asset/html/%s", file));

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case android.R.id.home:
        onBackPressed();
        return true;
}
return super.onOptionsItemSelected(item);
    }
}

您的问题是,
home\u layout
有一个
match\u parent
layouthweight
,这意味着它将消耗其父级的所有高度,而不给您任何提示

而是使用
wrap\u content
并指定
android:layoutwweight=“1”
来告诉它消耗所有未使用的空间。乙二醇

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<LinearLayout 
    android:id="@+id/home_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:id="@+id/progressBar" />
</LinearLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

</LinearLayout>

另请注意:

  • 上面的
    layout\u
    除了在相对布局中之外没有任何意义
  • fill\u parent
    已被弃用,请改用
    match\u parent
  • 您的WebView也有同样的问题

  • 你试着编译的时候有错误吗?与您的活动相对应的Java代码是什么?请重试并按照以下说明进行操作:@cypienaubry已使用活动代码进行更新。我可以在其他视图上显示广告,所以没问题。编译没有显示错误:)好的,我现在明白了,您没有实例化AdView。按照我在上一条评论中给出的链接上的所有说明重试;)@好的,我试试看。但是在其他视图中,我没有引用AdView,广告显示得很好。呵呵,安装得和你一样,但同样的事情:(没有添加。你在Gradle配置文件中添加依赖项吗?是的,我使用的是7.5版:compile'com.google.android.gms:play services广告:7.5.0
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    <LinearLayout 
        android:id="@+id/home_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:id="@+id/progressBar" />
    </LinearLayout>
    
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
    
    </LinearLayout>