【问题标题】:Make fragment totally inside framelayout使片段完全在框架布局内
【发布时间】: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


    【解决方案1】:

    试试这个:

     <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 属性以使框架布局显示在广告片段上方。

    另一种方法是使用垂直线性布局,首先使用框架布局,布局权重为 1(并且广告片段没有布局权重)。

    顺便说一下,片段完全包含在您的框架布局中。框架布局刚刚被原始布局中的广告片段重叠。上面的建议使得它没有重叠。

    希望这会有所帮助。

    【讨论】:

    • 谢谢,添加此行后工作android:layout_above="@id/adFragment
    【解决方案2】:

    如果你想避免重叠并让容器填满屏幕,那么你应该将RelativeLayout更改为LinearLayout。

     <LinearLayout
        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"
        android:orientation:"vertical"
        >
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"  />
        <fragment
            android:id="@+id/adFragment"
            android:name="com.jiyuzhai.wangxizhishufazidian.MainActivity$AdFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多