AdMob未显示。没有足够的空间来展示广告(片段文件)

时间:2018-09-30 23:45:31

标签: android android-layout admob

我希望一切都好。

我试图将AdMob广告添加到我的应用程序中。我想在WebView下方添加横幅。

没有显示广告,日志看起来没有足够的空间来显示横幅。

  

广告:没有足够的空间来展示广告。需要320x50 dp,但只有   0x561 dp。

     

广告:没有足够的空间来展示广告。需要320x50 dp,但只有   0x505 dp。

我的 SingleNewsArticleFragment 是:

package com.xx.app;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ecloud.pulltozoomview.PullToZoomScrollViewEx;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.firebase.appindexing.Action;
import com.google.firebase.appindexing.FirebaseUserActions;
import com.google.firebase.appindexing.builders.Actions;
import com.mikepenz.fontawesome_typeface_library.FontAwesome;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.view.IconicsTextView;
import com.almowaten.helpers.OnSwipeTouchListener;
import com.almowaten.helpers.PrettyTime;
import com.squareup.picasso.Picasso;

import static com.almowaten.news.R.id.descriptionView;

import android.support.annotation.NonNull;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.appindexing.FirebaseAppIndex;
import com.google.firebase.appindexing.Indexable;

/**
 * Created by melvin on 03/06/2017.
 */

public class SingleNewsArticleFragment extends Fragment {
    private AdView mAdView;
    private AdRequest adRequest;

    private static final String TAG = "SingleNewsArticleFragment";
    TextView titleView, dateView, breakingView;
    WebView textView;
    NewsArticle newsArticle;
    PullToZoomScrollViewEx scrollView;
    ImageView propertyImage;
    IconicsTextView photosView;

    int ItemId;
    boolean isFeatureImage = true;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setHasOptionsMenu(true);
        ViewGroup fragmentContentView;
        ViewGroup contentView;
        final View rootview = inflater.inflate(R.layout.article_content_view, container, true);
        mAdView = rootview.findViewById(R.id.adView2);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        //is feature image enabled
        isFeatureImage = Preference.getCached(getContext(), "showfeatureimage").equals("1");

        if (isFeatureImage) {
            //Feature image is enabled so show image on top
            fragmentContentView = (ViewGroup) inflater.inflate(R.layout.fragment_single_news_feature_img, container, false);

            //set zoom, content and header view
            scrollView = (PullToZoomScrollViewEx) fragmentContentView.findViewById(R.id.scroll_view);
            View headView = LayoutInflater.from(getContext()).inflate(R.layout.article_head_view, null, false);
            View zoomView = LayoutInflater.from(getContext()).inflate(R.layout.article_zoom_view, null, false);
            contentView = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.article_content_view, null, false);
            scrollView.setHeaderView(headView);
            scrollView.setZoomView(zoomView);
            scrollView.setScrollContentView(contentView);

            //set aspect ratio of header image
            DisplayMetrics localDisplayMetrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
            int mScreenWidth = localDisplayMetrics.widthPixels;
            LinearLayout.LayoutParams localObject = new LinearLayout.LayoutParams(mScreenWidth, (int) (9.0F * (mScreenWidth / 16.0F)));
            scrollView.setHeaderLayoutParams(localObject);

            //get article image element
            propertyImage = (ImageView) zoomView.findViewById(R.id.image);

            //handle image swipe/clicks
            propertyImage.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {

                @Override
                public void onClick() {
                    openFullScreenImage();

                }

                @Override
                public void onSwipeLeft() {

                }

                @Override
                public void onSwipeRight() {
                }

            });

            //photos counter
            photosView = (IconicsTextView) headView.findViewById(R.id.photos);
        } else {
            //feature image not enabled. So just show the content view.
            fragmentContentView = contentView = (ViewGroup) inflater.inflate(R.layout.article_content_view, container, false);
        }


        //get article prices elements
        titleView = (TextView) contentView.findViewById(R.id.title);
        dateView = (TextView) contentView.findViewById(R.id.dateView);
        breakingView = (TextView) contentView.findViewById(R.id.breaking);

        //set description view
        textView = (WebView) contentView.findViewById(descriptionView);
        textView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
            }
        });
        //enable javascript
        textView.getSettings().setJavaScriptEnabled(true);
        textView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != null && url.startsWith("http")) {
                    view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                } else {
                    return false;
                }
            }

        });

        //get item
        ItemId = getArguments().getInt(SingleNewsArticleActivity.ITEM_KEY);

        //comment button
        final Button commentsBtn = (Button) contentView.findViewById(R.id.commentsBtn);
        commentsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // launching Facebook comments activity
                Intent intent = new Intent(getActivity(), CommentsActivity.class);
                intent.putExtra("url", Configurations.SERVER_URL + ItemId);
                startActivity(intent);
            }
        });

        //load article
        NewsArticle.loadSingle(getActivity(), ItemId, new NewsArticle.onDownloadedListener() {
            @Override
            public void onDownloaded(NewsArticle newsArticleLocal) {
                if (getActivity() != null) {
                    newsArticleLocal.viewed(getContext());
                    newsArticle = newsArticleLocal;

                    //set title and text
                    titleView.setText(newsArticle.name);
                    textView.loadData(Functions.HTMLTemplate(newsArticle.text), "text/html; charset=utf-8", "utf-8");

                    //author
                    String author = "";
                    if (Preference.getCached(getContext(), "showauthorname").equals("1")) {
                        if (newsArticle.authorName.length() > 0)
                            author = " - " + newsArticle.authorName;
                    }

                    //date
                    String DateToStr = PrettyTime.getTimeAgo(newsArticle.submission_date, "yyyy-MM-dd HH:mm:ss", getActivity());
                    dateView.setText(DateToStr + author);

                    if (newsArticle.allowComments == 1)
                        commentsBtn.setVisibility(View.VISIBLE);
                    else
                        commentsBtn.setVisibility(View.GONE);

                    //breaking
                    if (newsArticle.is_breaking == 1) {
                        breakingView.setVisibility(View.VISIBLE);
                    } else {
                        breakingView.setVisibility(View.GONE);
                    }

                    //set image if in feature mode
                    if (isFeatureImage) {
                        //set image
                        if (newsArticle.imageUrl != null) {
                            Picasso.with(getContext())
                                    .load(newsArticle.imageUrl[0])
                                    .fit()
                                    .placeholder(R.drawable.loading)
                                    .into(propertyImage);
                        }

                    }
                    logDeepLinkAction(newsArticle.name,Configurations.SERVER_URL + newsArticle.id);
                    indexNewsContent(newsArticle.name,newsArticle.text,Configurations.SERVER_URL + newsArticle.id);
                }
            }
        });
        return fragmentContentView;
    }


    /**
     * Open images in full screen mode
     */
    public void openFullScreenImage() {
        if(newsArticle==null)
            return;
        Bundle b = new Bundle();
        b.putStringArray("imageUrl", newsArticle.imageUrl);
        b.putInt("slideshow_seconds", Configurations.SLIDESHOW_TIME_SECONDS);
        Intent i = new Intent(getContext(), FullScreenImage.class);
        i.putExtras(b);
        startActivity(i);
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.single_news_menu, menu);

        //set share icon from FontAwsome
        menu.findItem(R.id.share).setIcon(
                new IconicsDrawable(getContext())
                        .icon(FontAwesome.Icon.faw_share_alt)
                        .color(ContextCompat.getColor(getContext(), R.color.md_white_1000))
                        .sizeDp(18));

        //set bookmark icon from FontAwesome
        boolean isFavorite = (NewsArticle.isFavoriteById(getActivity(), ItemId));
        if (isFavorite) {
            menu.findItem(R.id.bookmark).setIcon(new IconicsDrawable(getActivity())
                    .icon(FontAwesome.Icon.faw_bookmark)
                    .color(ContextCompat.getColor(getActivity(), R.color.md_white_1000))
                    .sizeDp(18));
        } else {
            menu.findItem(R.id.bookmark).setIcon(new IconicsDrawable(getActivity())
                    .icon(FontAwesome.Icon.faw_bookmark_o)
                    .color(ContextCompat.getColor(getActivity(), R.color.md_white_1000))
                    .sizeDp(18));

        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle share
        switch (item.getItemId()) {
            case R.id.share:
                NewsArticle.shareById(getActivity(), ItemId);
                return true;

            case R.id.bookmark:
                boolean isFavorite = (NewsArticle.isFavoriteById(getActivity(), ItemId));
                NewsArticle.setFavoriteById(getActivity(), !isFavorite, ItemId);
                if (isFavorite) {
                    item.setIcon(new IconicsDrawable(getActivity())
                            .icon(FontAwesome.Icon.faw_bookmark_o)
                            .color(ContextCompat.getColor(getActivity(), R.color.md_white_1000))
                            .sizeDp(18));
                } else {
                    item.setIcon(new IconicsDrawable(getActivity())
                            .icon(FontAwesome.Icon.faw_bookmark)
                            .color(ContextCompat.getColor(getActivity(), R.color.md_white_1000))
                            .sizeDp(18));
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public void onPause() {
        textView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        textView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        textView.destroy();

    }

    private void indexNewsContent(final String name,String desc, String url) {

        Indexable contentIndex = new Indexable.Builder()
                .setName(name)
                .setUrl(url)
                .setDescription(desc)
                .build();

        Task<Void> task =
                FirebaseAppIndex.getInstance().update(contentIndex);

        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d("Content Indexing", "App Indexing added "
                        + name + " to " +
                        "index");
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Log.e("Content Indexing", "App Indexing failed to add " +
                        name + " to index. " +
                        "" + exception.getMessage());
            }
        });
    }

    private void logDeepLinkAction(final String title, final String url) {
        Action action = Actions.newView(title, url);
        FirebaseUserActions.getInstance().end(action);
    }

}

我的布局是:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:descendantFocusability="beforeDescendants"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorWhiteBackground"
        android:orientation="vertical">

        <TextView
            android:id="@+id/breaking"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="0dp"
            android:background="@color/color_breaking_news"
            android:gravity="start"
            android:paddingBottom="3dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="3dp"
            android:text="@string/breaking"
            android:textAlignment="gravity"
            android:textColor="@color/colorWhite"
            android:textDirection="locale"
            android:textSize="14sp"
            android:visibility="gone" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:textColor="@color/colorText"
            android:textSize="24sp" />


        <TextView
            android:id="@+id/dateView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="8dp"
            android:textColor="@color/colorSecondaryText"
            android:textSize="12sp" />

        <WebView
            android:id="@+id/descriptionView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="25dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="4dp"
            android:textColor="@color/colorText"
            android:textSize="14sp" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad">
    </com.google.android.gms.ads.AdView>

        <Button
            android:visibility="gone"
            android:layout_margin="30dp"
            android:textSize="14sp"
            android:textStyle="bold"
            android:textAllCaps="true"
            android:textColor="@color/md_white_1000"
            android:background="@color/accent"
            android:id="@+id/commentsBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/comments" />


    </LinearLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:0)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    <ScrollView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:ads="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:descendantFocusability="beforeDescendants"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorWhiteBackground"
        android:orientation="vertical">

        <TextView
            android:id="@+id/breaking"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="0dp"
            android:background="@color/color_breaking_news"
            android:gravity="start"
            android:paddingBottom="3dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="3dp"
            android:text="@string/breaking"
            android:textAlignment="gravity"
            android:textColor="@color/colorWhite"
            android:textDirection="locale"
            android:textSize="14sp"
            android:visibility="gone" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:textColor="@color/colorText"
            android:textSize="24sp" />


        <TextView
            android:id="@+id/dateView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="8dp"
            android:textColor="@color/colorSecondaryText"
            android:textSize="12sp" />

        <WebView
            android:id="@+id/descriptionView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="25dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="4dp"
            android:textColor="@color/colorText"
            android:textSize="14sp" />


        <Button
            android:visibility="gone"
            android:layout_margin="30dp"
            android:textSize="14sp"
            android:textStyle="bold"
            android:textAllCaps="true"
            android:textColor="@color/md_white_1000"
            android:background="@color/accent"
            android:id="@+id/commentsBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/comments" />


    </LinearLayout>
</ScrollView>

 <com.google.android.gms.ads.AdView
        android:id="@+id/adView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

答案 1 :(得分:0)

您的admob标记在xml文件中应如下所示:

<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" />

,它应该在您的滚动视图标签下方。