Android fragments 在android片段中插入admob? package htm.greensoftware.com; 导入com.google.android.gms.ads.AdRequest; 导入com.google.android.gms.ads.AdSize; 导入com.google.android.gms.ads.AdView; 导入android.annotation.SuppressLint; 导入android.app.Fragment; 导入android.os.Bundle; 导入android.view.LayoutInflater; 导入android.view.view; 导入android.view.ViewGroup; 导入android.webkit.WebView; 导入android.widget.LinearLayout; 公共类HomeFragment扩展了片段{ 网络视图; 私人咨询咨询; 私人信件; 公共HomeFragment(){ } @SuppressLint(“新API”) @凌驾 创建视图上的公共视图(布局、充气机、视图组容器、, Bundle savedInstanceState){ 视图根视图=充气机。充气(R.layout.fragment_home,容器, 假); wv=(WebView)rootView.findviewbyd(R.id.webView1); wv.loadUrl(“file:///android_asset/index.html"); adView=newadview(getActivity()); 咨询意见。setAdUnitId(“ca-app-pub-5456867664363319/8478541984”); adView.setAdSize(AdSize.BANNER); LinearLayout布局=(LinearLayout)rootView.findviewbyd(R.id.ll_ad); layout.addView(adView); AdRequest AdRequest=新建AdRequest.Builder().build(); adView.loadAd(adRequest); 返回rootView; } }

Android fragments 在android片段中插入admob? package htm.greensoftware.com; 导入com.google.android.gms.ads.AdRequest; 导入com.google.android.gms.ads.AdSize; 导入com.google.android.gms.ads.AdView; 导入android.annotation.SuppressLint; 导入android.app.Fragment; 导入android.os.Bundle; 导入android.view.LayoutInflater; 导入android.view.view; 导入android.view.ViewGroup; 导入android.webkit.WebView; 导入android.widget.LinearLayout; 公共类HomeFragment扩展了片段{ 网络视图; 私人咨询咨询; 私人信件; 公共HomeFragment(){ } @SuppressLint(“新API”) @凌驾 创建视图上的公共视图(布局、充气机、视图组容器、, Bundle savedInstanceState){ 视图根视图=充气机。充气(R.layout.fragment_home,容器, 假); wv=(WebView)rootView.findviewbyd(R.id.webView1); wv.loadUrl(“file:///android_asset/index.html"); adView=newadview(getActivity()); 咨询意见。setAdUnitId(“ca-app-pub-5456867664363319/8478541984”); adView.setAdSize(AdSize.BANNER); LinearLayout布局=(LinearLayout)rootView.findviewbyd(R.id.ll_ad); layout.addView(adView); AdRequest AdRequest=新建AdRequest.Builder().build(); adView.loadAd(adRequest); 返回rootView; } },android-fragments,admob,Android Fragments,Admob,我用谷歌搜索了一下,但没有找到答案。 我尝试了以下链接。 你的问题不清楚,但是 是的,您可以在片段中显示AdView 是的,您的配置看起来大致正确 您已将WebView标记为android:layout\u height=“fill\u parent”,因此它将获取所有屏幕高度,而您的AdView则没有。如果要扩展以填充未使用的空间,则需要使用包含的线性布局并使用布局\u weight=“1”,以指示它应该扩展 <?xml version="1.0" encoding="utf-8"?&g

我用谷歌搜索了一下,但没有找到答案。 我尝试了以下链接。
你的问题不清楚,但是

  • 是的,您可以在片段中显示AdView
  • 是的,您的配置看起来大致正确
  • 您已将WebView标记为android:layout\u height=“fill\u parent”,因此它将获取所有屏幕高度,而您的AdView则没有。如果要扩展以填充未使用的空间,则需要使用包含的线性布局并使用
    布局\u weight=“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;
    
        }
    
    }