【发布时间】:2015-05-15 03:38:15
【问题描述】:
在我的应用中,我想在所有屏幕的底部保留一个相同的广告横幅,因此我使用一个包含多个片段的 Activity。
在 Activity 的 layoutfile(activity_mail.xml) 中,我有一个 FrameLayout 作为片段的容器,底部有一个 AdView,用于显示来自 Admob 的横幅广告。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/adFragment"
android:name="com.jiyuzhai.wangxizhishufazidian.MainActivity$AdFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
替换现有片段的片段布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/theme_color"
android:layout_alignParentTop="true"
android:text="Topbar in fragment"
android:textColor="@android:color/white"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/theme_color"
android:layout_alignParentBottom="true"
android:text="Bottombar in fragment"
android:textColor="@android:color/white"
android:textSize="30sp"/>
</RelativeLayout>
替换现有片段的代码
fragment = new LinmoFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out);
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
但是当我替换容器中的片段时,片段的底部区域被 AdView 隐藏,换句话说,片段超出了 Framelayout,我希望片段完全在容器内。这可能吗?
以下是我想要的
这就是我得到的(广告横幅隐藏了片段的底部栏)
有什么想法吗?
顺便说一句,我发现没有办法在 Android 中为具有多个活动的所有屏幕保留相同的横幅,SO 上的许多人说您需要使用具有多个片段的单个活动,然后您可以添加/删除您的片段动态不重新加载新横幅,但是,没有找到这种方法的代码,所以我自己试试。如果您有更好的解决方案,请帮助我。
【问题讨论】:
标签: android android-fragments android-framelayout