首页 > 解决方案 > 如何避免admob adview被切断

问题描述

我有一个Activity两个片段,一个在顶部发生变化,另一个是始终固定在屏幕底部的 admob adview。神奇的是,当我现在向您展示的片段中按下后退按钮时,它会被切断:

public class FragmentChallengeFullDecription extends Fragment {

    private Challenge challengeClicked = null;
    private ImageView challengeImage;
    private TextView challengeDescription;

    public FragmentChallengeFullDecription() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_challenge_full_decription, container, false);


        Bundle bundle = getArguments();

        if (bundle != null) {
            challengeClicked = bundle.getParcelable("CHALLENGE_CLICKED");
        }

        CollapsingToolbarLayout collapsingToolbar = view.findViewById(R.id.collapsingToolbar);
        collapsingToolbar.setTitle(challengeClicked.getName());


        challengeImage = view.findViewById(R.id.challengeImage);
        challengeImage.setImageResource(challengeClicked.getImage());


        challengeDescription = view.findViewById(R.id.challengeDescription);
        challengeDescription.setText(challengeClicked.getDescription());


        return view;
    }
}

这是显示错误的视频: https ://imgur.com/gallery/pRFhG9e?s=wa

您是否认为问题可能与CollapsingToolbarLayout相关,我听说它非常有问题?我迷路了。

根据需要,这是xml包含折叠工具栏布局的最后一个片段的文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentChallengeFullDecription"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:expandedTitleMarginStart="40dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >

        <ImageView
            android:id="@+id/challengeImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher_background"
            app:layout_collapseMode="parallax"/>          

        <include layout="@layout/toolbar"
            android:id="@+id/myToolbar"/>        

    </android.support.design.widget.CollapsingToolbarLayout>



</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:elevation="5dp"
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:paddingBottom="?attr/actionBarSize"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="vertical"
        android:paddingTop="10dp"
        >

        <TextView
            android:id="@+id/challengeDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Description"/>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

这是包含 adview 的片段的 xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintBottom_toBottomOf="parent"
        >
    </com.google.android.gms.ads.AdView>


</FrameLayout>

这就是我将 adview 片段添加到一般活动的方式:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bannerFragment"
        >

    </FrameLayout>


    <fragment
        android:id="@+id/bannerFragment"
        android:name="com.example.silentlibrary.FragmentBanner"
        tools:layout="@layout/fragment_banner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />
</android.support.constraint.ConstraintLayout>

标签: androidandroid-fragmentsadmobadview

解决方案


推荐阅读