2016-04-26 42 views
1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:background="@drawable/background" 
    android:gravity="bottom" > 

    <WebView 
     android:id="@+id/webView1" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:textAlignment="gravity" /> 

    <LinearLayout 
     android:id="@+id/ll_ad" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" > 

     <com.google.android.gms.ads.AdView 
      android:id="@+id/adView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      ads:adSize="BANNER" 
      ads:adUnitId="ca-app-pub-5456867664363319/8478541984" > 
     </com.google.android.gms.ads.AdView> 
    </LinearLayout> 

</RelativeLayout> 

package htm.greensoftware.com; 

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

import android.annotation.SuppressLint; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebView; 
import android.widget.LinearLayout; 

public class HomeFragment<ChildFragment> extends Fragment { 

    WebView wv; 
    private AdView adView; 
    private AdRequest adRequest; 

    public HomeFragment() { 
    } 

    @SuppressLint("NewApi") 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_home, container, 
       false); 

     wv = (WebView) rootView.findViewById(R.id.webView1); 
     wv.loadUrl("file:///android_asset/index.html"); 
     adView = new AdView(getActivity()); 

     adView.setAdUnitId("ca-app-pub-5456867664363319/8478541984"); 
     adView.setAdSize(AdSize.BANNER); 
     LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.ll_ad); 
     layout.addView(adView); 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     adView.loadAd(adRequest); 
     return rootView; 

    } 

} 

我GOOGLE了,但沒有適當的不能得到答案。 我試過下面的鏈接。 AdMob in a fragment Using AdMob from within a Fragment? Android Admob ad not shown in Activity with fragment. Why?在android片段中插入admob?

回答

0

目前尚不清楚你的問題是什麼,但

  1. 是的,你可以在一個片段
  2. 是顯示AdView的,你的配置看起來大致正確。
  3. 您已將您的WebView標記爲android:layout_height="fill_parent",因此它將抓取所有屏幕高度,而不會讓您的AdView無法顯示。如果您希望我擴大以填充未使用的空間,那麼您將需要使用包含LinearLayout並使用layout_weight="1"以指示它應該展開。