gpt4 book ai didi

android - 使 fragment 完全在框架布局内

转载 作者:行者123 更新时间:2023-11-29 17:36:32 25 4
gpt4 key购买 nike

在我的应用中,我想在所有屏幕的底部保留一个相同的广告横幅,所以我使用一个包含多个 fragment 的 Activity。

在 Activity 的布局文件 (activity_mail.xml) 中,我有一个 FrameLayout 作为 fragment 的容器,底部有一个 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>

替换现有 fragment 的 fragment 布局

<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 的代码

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();

但是当我替换容器中的 fragment 时, fragment 的底部区域被AdView隐藏了,换句话说, fragment 在Framelayout之外,我希望 fragment 完全在容器内。这可能吗?

下面是我想要的

enter image description here

这就是我得到的(广告横幅隐藏了 fragment 的底部栏)

enter image description here

有什么想法吗?

顺便说一下,我发现没有办法在 Android 中为具有多个 Activity 的所有屏幕保留相同的横幅,SO 上的许多人说您需要使用具有多个 fragment 的 Single Activity,然后您可以添加/删除 fragment 动态而不重新加载新横幅,但是,没有找到这种方法的代码,所以我自己试试。如果您有更好的解决方案,请帮助我。

最佳答案

试试这个:

 <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">
<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" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/adFragment" />
</RelativeLayout>

我对您的原始布局文件所做的更改是将 adFragment 定义移动到框架布局上方,使框架布局高度包裹内容,并添加 layout_above 属性以使框架布局显示在广告 fragment 。

另一种方法是使用垂直线性布局,框架布局首先将布局权重设置为 1(并且广告 fragment 不会有布局权重)。

顺便说一句,该 fragment 完全包含在您的框架布局中。框架布局只是与原始布局中的广告 fragment 重叠。上面的建议使得没有重叠。

希望这对您有所帮助。

关于android - 使 fragment 完全在框架布局内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30251119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com