我在我的应用中使用PublisherAdView
进行广告整合。每个视图我有3个广告,位于ViewPager
内。广告是最初可见的,但在访问ViewPager
中的特定数量的片段后,广告会停止工作,并在日志中显示:
I/Ads: Ad is not visible. Not refreshing ad.
I/Ads: Scheduling ad refresh 60000 milliseconds from now.
为所有,直到app重新启动。我还为所有publsihersAdViews实施了onPause()
,onResume()
和onDestroy()
方法。
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="com.expressdigital.express.fragments.NewsDetailFragment">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/news_details_swipe_to_refresh">
<android.support.v4.widget.NestedScrollView
android:id="@+id/news_details_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:fillViewport="true"
android:scrollbars="none"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:id="@+id/news_details_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<!-- top ad layout-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/news_details_top_ad_view_layout"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/news_details_screen_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/news_details_top_ad_view_layout"
android:layout_marginTop="10dp"
android:lineSpacingMultiplier="1.2"
android:paddingLeft="@dimen/news_details_title_left_padding"
android:paddingRight="@dimen/news_details_title_right_padding"
android:textColor="@color/black"
android:textSize="@dimen/news_details_heading_text_size"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/author_name_and_time_passed_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/news_details_screen_title"
android:orientation="horizontal"
android:paddingBottom="@dimen/author_time_layout_vertical_padding"
android:paddingTop="@dimen/author_time_layout_vertical_padding">
<TextView
android:id="@+id/details_author_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/news_details_author_name_left_margin"
android:layout_marginRight="@dimen/news_details_author_name_right_margin"
android:textColor="@color/login_top"
android:textSize="@dimen/news_details_author_name_text_size" />
<com.expressdigital.express.utils.CircleImageView
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_gravity="center_vertical"
android:src="@color/divider_bottom" />
<TextView
android:id="@+id/details_publish_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/news_details_time_horizontal_margin"
android:layout_marginRight="@dimen/news_details_time_horizontal_margin"
android:textAllCaps="true"
android:textColor="@color/login_top"
android:textSize="@dimen/news_details_time_text_size" />
</LinearLayout>
<ImageView
android:id="@+id/news_description_top_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/author_name_and_time_passed_layout"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<WebView
android:id="@+id/news_description_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/news_description_top_image"
android:layout_marginLeft="@dimen/news_details_content_horizontal_padding"
android:layout_marginRight="@dimen/news_details_content_horizontal_padding"
android:layout_marginTop="@dimen/news_details_content_vertical_padding" />
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_below="@id/news_description_detail"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/view_above_mrec_ad"
android:background="@color/divider_bottom"
/>
<!-- bottom ad layout-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/news_details_bottom_ad_view_layout"
android:layout_centerHorizontal="true"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_below="@id/view_above_mrec_ad"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_below="@id/news_details_bottom_ad_view_layout"
android:id="@+id/extraSection">
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_below="@id/news_description_detail"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/view_above_comments_layout"
android:background="@color/divider_bottom"
/>
<LinearLayout
android:id="@+id/details_comments_buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/comment_buttons_layout_bottom_margin"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:id="@+id/details_add_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/news_details_add_comment_horizontal_padding"
android:layout_marginRight="@dimen/news_details_add_comment_horizontal_padding"
android:layout_weight="1"
android:paddingTop="@dimen/comments_buttons_vertical_padding"
android:paddingBottom="@dimen/comments_buttons_vertical_padding"
android:paddingLeft="@dimen/comments_buttons_horizontal_padding"
android:paddingRight="@dimen/comments_buttons_horizontal_padding"
android:gravity="center"
android:textAllCaps="true"
android:textSize="@dimen/news_details_comments_button_text_size"
android:background="@drawable/add_comment_button_background"
android:text="@string/add_comment"
android:textColor="@color/read_all" />
<TextView
android:id="@+id/details_read_comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/news_details_comment_horizontal_padding"
android:layout_marginRight="@dimen/news_details_comment_horizontal_padding"
android:layout_weight="1"
android:paddingTop="@dimen/comments_buttons_vertical_padding"
android:paddingBottom="@dimen/comments_buttons_vertical_padding"
android:paddingLeft="@dimen/comments_buttons_horizontal_padding"
android:paddingRight="@dimen/comments_buttons_horizontal_padding"
android:gravity="center"
android:textAllCaps="true"
android:textSize="@dimen/news_details_comments_button_text_size"
android:text="@string/read_comments"
android:background="@drawable/read_comments_background"
android:textColor="@color/white" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_below="@id/details_comments_buttons_layout"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/divider_bottom"
android:id="@+id/view_below_comments_layout"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/details_next_story_layout"
android:layout_below="@id/view_below_comments_layout"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/details_next_story_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/details_comments_buttons_layout"
android:paddingLeft="@dimen/news_details_recommend_text_horizontal_padding"
android:paddingRight="@dimen/news_details_recommend_text_horizontal_padding"
android:text="@string/next_story"
android:textColor="@color/we_recommend"
android:textSize="@dimen/we_recommend_text_size"
android:textAllCaps="true"
android:textStyle="bold"
android:paddingBottom="10dp" />
<TextView
android:id="@+id/details_next_story_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/details_comments_buttons_layout"
android:paddingLeft="@dimen/news_details_recommend_text_horizontal_padding"
android:paddingRight="@dimen/news_details_recommend_text_horizontal_left_padding"
android:text="@string/next_story"
android:textSize="@dimen/story_heading_text_size"
android:paddingBottom="10dp" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/next_story_icon"
android:layout_weight="8"
android:layout_gravity="center_vertical"
android:padding="10dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_below="@id/details_next_story_layout"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/divider_bottom"
android:id="@+id/view_below_next_story"
/>
<View
android:id="@+id/view_for_detail_news_lazy_loading"
android:layout_width="match_parent"
android:layout_height="@dimen/news_details_blank_views_below_comment_layout_height"
android:layout_below="@id/news_description_detail"
android:layout_marginTop="@dimen/news_details_blank_views_below_comment_layout_top_margin"
android:visibility="gone"
android:background="@android:color/transparent" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/news_details_bottom_progress_layout_width"
android:layout_height="@dimen/news_details_bottom_progress_layout_height"
android:layout_centerInParent="true"
android:gravity="center"
android:layout_below="@id/view_below_next_story"
android:id="@+id/detail_news_progress_bar"
>
<com.expressdigital.express.utils.CircleImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/express_loader"
android:alpha="0.7"
android:layout_marginRight="@dimen/news_details_bottom_logo_padding"
android:layout_marginLeft="@dimen/news_details_bottom_logo_padding"
android:layout_margin="@dimen/news_details_bottom_logo_padding"
android:layout_marginBottom="@dimen/news_details_bottom_logo_padding"
/>
<ProgressBar
android:indeterminate="true"
android:layout_width="@dimen/news_details_bottom_progress_layout_width"
android:layout_height="@dimen/news_details_bottom_progress_layout_height"
style="@style/CustomProgressBar"
/>
</RelativeLayout>
<TextView
android:id="@+id/details_we_recommend_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/view_for_detail_news_lazy_loading"
android:paddingLeft="@dimen/news_details_recommend_text_horizontal_padding"
android:paddingRight="@dimen/news_details_recommend_text_horizontal_padding"
android:text="@string/related_stories"
android:textColor="@color/we_recommend"
android:textSize="@dimen/we_recommend_text_size"
android:textAllCaps="true"
android:textStyle="bold"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="@+id/details_we_recommend_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/details_we_recommend_text"
android:foregroundGravity="center"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:visibility="gone" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/extraSection"
android:background="@color/divider_bottom"
android:id="@+id/view_above_details_ad_view"
/>
<!-- ad type mrec layout-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/news_details_ad_view"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_centerHorizontal="true"
android:layout_below="@id/view_above_details_ad_view"
/>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_below="@id/news_details_ad_view"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/divider_bottom"
android:id="@+id/view_below_details_ad_view"
/>
</RelativeLayout>
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/news_details_progress_layout"
android:layout_marginBottom="@dimen/actionbar_size">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/actionbar_size">
<RelativeLayout
android:layout_width="@dimen/news_details_progress_layout_width"
android:layout_height="@dimen/news_details_progress_layout_height"
android:layout_centerInParent="true"
android:gravity="center"
>
<com.expressdigital.express.utils.CircleImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/express_loader"
android:alpha="0.7"
android:layout_marginRight="@dimen/news_details_logo_padding"
android:layout_marginLeft="@dimen/news_details_logo_padding"
android:layout_margin="@dimen/news_details_logo_padding"
android:layout_marginBottom="@dimen/news_details_logo_padding"
/>
<ProgressBar
android:layout_width="@dimen/news_details_progress_layout_width"
android:layout_height="@dimen/news_details_progress_layout_height"
style="@style/CustomProgressBar"
/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
当您在viewPager
中使用广告时,您的布局会遇到一些问题。
因为您正在使用RelativeLayout,所以将横幅示例代码替换为
// Create the adView
AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
// Lookup your RelativeLayoutLayout assuming it’s been given
// the attribute android:id="@+id/ad"
RelativeLayoutlayout layout = (RelativeLayout)findViewById(R.id.ad);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
其他解决方案
AdView adView = (AdView) findViewById(R.id.ad);
adView.bringToFront();