2015-11-16 47 views
1

我在Android開發方面的新的,所以要求你的幫助。添加的AdBlock到佈局的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> 

以我的邏輯,廣告攔截應的視圖底部顯示。但在我的情況下,它根本沒有出現。如果我在LinearLayout之內通過Ad的標記,它會讓我崩潰。

我在做什麼錯了?謝謝你在前進

UPD:Java的活動

public class PddViewActivity extends ActionBarActivity{ 

private WebView webView; 
private ProgressBar progressBar; 

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

    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); 
} 
} 
+0

你有任何錯誤,當您嘗試編譯?與您的活動相對應的Java代碼是什麼? –

+0

谷歌的指示是什麼? –

+0

重試,並按照這些指令:[AdMob的快速啓動(https://developers.google.com/admob/android/quick-start) –

回答

1

您的問題是home_layoutmatch_parent一個layoutHeight,這意味着它會消耗其父母的所有高度,不會爲您的廣告查看。

而是使用wrap_content並指定android:layoutWeight="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> 

還要注意:

  1. layout_above已經沒有實際意義,只是一個relative_layout內。
  2. fill_parent已被棄用,使用match_parent代替。
  3. 您的WebView有同樣的問題。
+0

這樣做了,謝謝。 – inffy

1

試着與你的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); 
    } 
} 
+0

嘿嘿,instactiated像你一樣,但同樣的事情:(上有沒有增加 – inffy

+0

你在搖籃配置文件中添加依賴 –

+1

葉氏,使用IM版本7.5:?編譯「com.google.android.gms:玩-services-ads:7.5.0' – inffy