2014-02-25 43 views
1

我正在使用HTML5WebView。Android将AdView添加到HTML5WebView中

https://code.google.com/p/html5webview/source/browse/#svn%2Ftrunk%2FHTML5WebView%253Fstate%253Dclosed

我加入谷歌的AdView在HTML5WebView的底部,但AdView后,显示在HTML5WebView的顶了过来。我希望HTML5WebView在拍摄AdView后仅拍摄屏幕的其余部分。

我可以知道如何更改我的布局。我已经尝试过,但没有运气。这是我最新的结果,AdView与HTML5WebView重叠。

谢谢, 亚历克斯

这里是我的

custom_screen.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"> 

    <FrameLayout android:id="@+id/fullscreen_custom_content" 
     android:visibility="gone" 
     android:background="@color/black" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" >   
     <ProgressBar 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="match_parent" 
      android:layout_height="5dp" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:indeterminate="true" 
       android:id="@+id/webViewProgressBar"/> 

     <LinearLayout 
      android:id="@+id/error_console" 
      android:orientation="vertical"   
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
     /> 
     <FrameLayout android:id="@+id/main_content" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
     />   
    </LinearLayout> 

    <com.google.ads.AdView   
      android:id="@+id/adView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      ads:adUnitId="a15302d1a9e05e7" 
      ads:adSize="BANNER" 
      ads:testDevices="TEST_EMULATOR" 
      ads:loadAdOnCreate="true"/> 

</FrameLayout> 

HTML5WebView.java

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.webkit.GeolocationPermissions; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.FrameLayout; 
import android.widget.ProgressBar; 

public class HTML5WebView extends WebView { 

    private Context               mContext; 
    private MyWebChromeClient            mWebChromeClient; 
    private View               mCustomView; 
    private FrameLayout              mCustomViewContainer; 
    private WebChromeClient.CustomViewCallback        mCustomViewCallback; 

    private FrameLayout              mContentView; 
    private FrameLayout              mBrowserFrameLayout; 
    private FrameLayout              mLayout; 
    private ProgressBar              progressBar; 
static final String LOGTAG = "HTML5WebView"; 

    @SuppressLint({ "SetJavaScriptEnabled", "SdCardPath" }) 
    @SuppressWarnings("deprecation") 
    private void init(Context context) { 
     mContext = context;    
     Activity a = (Activity) mContext; 

     mLayout = new FrameLayout(context); 

     mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(a).inflate(R.layout.custom_screen, null); 
     mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content); 
     mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.fullscreen_custom_content); 

     mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS); 
     progressBar = (ProgressBar) mBrowserFrameLayout.findViewById(R.id.webViewProgressBar); 
     mWebChromeClient = new MyWebChromeClient(); 
     setWebChromeClient(mWebChromeClient); 

     setWebViewClient(new MyWebViewClient(progressBar)); 

     // Configure the webview 
     WebSettings s = getSettings(); 
     s.setBuiltInZoomControls(true); 
     s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); 
     s.setUseWideViewPort(true); 
     s.setLoadWithOverviewMode(true); 
     s.setSavePassword(true); 
     s.setSaveFormData(true); 
     s.setJavaScriptEnabled(true); 

     // enable navigator.geolocation 
     s.setGeolocationEnabled(true); 
     s.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/"); 

     // enable Web Storage: localStorage, sessionStorage 
     s.setDomStorageEnabled(true); 

     mContentView.addView(this); 
    } 

    public HTML5WebView(Context context) { 
      super(context); 
      init(context); 
    } 

    public HTML5WebView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      init(context); 
    } 

    public HTML5WebView(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
      init(context); 
    } 

    public FrameLayout getLayout() { 
      return mLayout; 
    } 

public boolean inCustomView() { 
      return (mCustomView != null); 
    } 

public void hideCustomView() { 
      mWebChromeClient.onHideCustomView(); 
    } 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
      if ((mCustomView == null) && canGoBack()){ 
        goBack(); 
        return true; 
      } 
    } 
    return super.onKeyDown(keyCode, event); 
} 

    private class MyWebChromeClient extends WebChromeClient { 
     private Bitmap   mDefaultVideoPoster; 
     private View   mVideoProgressView; 


     @Override 
     public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) 
     { 
       //Log.i(LOGTAG, "here in on ShowCustomView"); 
      HTML5WebView.this.setVisibility(View.GONE); 

      // if a view already exists then immediately terminate the new one 
      if (mCustomView != null) { 
       callback.onCustomViewHidden(); 
       return; 
      } 

      mCustomViewContainer.addView(view); 
      mCustomView = view; 
      mCustomViewCallback = callback; 
      mCustomViewContainer.setVisibility(View.VISIBLE); 
     } 

     @Override 
     public void onHideCustomView() { 

       if (mCustomView == null) 
         return;   

       // Hide the custom view. 
       mCustomView.setVisibility(View.GONE); 

       // Remove the custom view from its container. 
       mCustomViewContainer.removeView(mCustomView); 
       mCustomView = null; 
       mCustomViewContainer.setVisibility(View.GONE); 
       mCustomViewCallback.onCustomViewHidden(); 

       HTML5WebView.this.setVisibility(View.VISIBLE); 

     //Log.i(LOGTAG, "set it to webVew"); 
     } 

     @Override 
     public Bitmap getDefaultVideoPoster() { 
       //Log.i(LOGTAG, "here in on getDefaultVideoPoster");  
       if (mDefaultVideoPoster == null) { 
         mDefaultVideoPoster = BitmapFactory.decodeResource(
             getResources(), R.drawable.default_video_poster); 
      } 
       return mDefaultVideoPoster; 
     } 

     @Override 
     public View getVideoLoadingProgressView() { 
       //Log.i(LOGTAG, "here in on getVideoLoadingPregressView"); 

     if (mVideoProgressView == null) { 
      LayoutInflater inflater = LayoutInflater.from(mContext); 
      mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null); 
     } 
     return mVideoProgressView; 
     } 

     @Override 
     public void onReceivedTitle(WebView view, String title) { 
      ((Activity) mContext).setTitle(title); 
     } 

     @Override 
     public void onProgressChanged(WebView view, int newProgress) { 
       ((Activity) mContext).getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100); 
     } 

     @Override 
     public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { 
      callback.invoke(origin, true, false); 
     } 
    } 

    private class MyWebViewClient extends WebViewClient { 
     private ProgressBar progressBar; 
     public MyWebViewClient(ProgressBar progressBar) { 
      this.progressBar=progressBar; 
      progressBar.setVisibility(View.VISIBLE); 
     } 
     @Override 

     public void onPageFinished(WebView view, String url) { 

      // TODO Auto-generated method stub 

      super.onPageFinished(view, url); 

      progressBar.setVisibility(View.GONE); 

     } 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      Log.i(LOGTAG, "shouldOverrideUrlLoading: "+url); 
      // don't override URL so that stuff within iframe can work properly 
      // view.loadUrl(url); 
      return false; 
     }  

    } 

    static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = 
    new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
} 
+0

使用此链接http://stackoverflow.com/questions/21479203/cant-ad-ads-to-my-app/21479521#21479521 –

+0

嗨迪帕克,感谢您的链接。我的问题是Adview重叠HTMLWebView。我认为这是因为HTMLWebView布局。现在,我的网页页脚与AdView重叠。因此,该用户无法点击页脚上的某个链接。我试过了上面的例子,但结果是一样的。 –

+0

嗨,alex.can你解决了这个问题? http://stackoverflow.com/questions/23812917/not-enough-ad-space-when-add-admob-in-html5webview –

回答

1

包装你的LinearLayout和AdView中,像这样:

<LinearLayout android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" >   

    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" >   
     <ProgressBar 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="match_parent" 
      android:layout_height="5dp" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:indeterminate="true" 
       android:id="@+id/webViewProgressBar"/> 

     <LinearLayout 
      android:id="@+id/error_console" 
      android:orientation="vertical"   
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
     /> 
     <FrameLayout android:id="@+id/main_content" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
     />   
    </LinearLayout> 

    <com.google.ads.AdView   
      android:id="@+id/adView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      ads:adUnitId="a15302d1a9e05e7" 
      ads:adSize="BANNER" 
      ads:testDevices="TEST_EMULATOR" 
      ads:loadAdOnCreate="true"/> 

</LinearLayout> 
+1

嗨威廉,非常感谢你。这正是我所要求的。 –

+0

这位先生呢? http://stackoverflow.com/questions/23812917/not-enough-ad-space-when-add-admob-in-html5webview –