显示一个片段在另一个片段之上。

3
基本上,我想要做的是:

Fragments lifecycle

    1.- 我有两个与活动“无关”的片段。
    2.- 现在可能有两种情况,片段1或片段2必须以向上滑动的动画显示。
    3.- 当两个片段都在屏幕上时,片段1总是在片段2的上方。
    4.- 任何一个片段都可以隐藏,将显示的片段正确地放置在屏幕底部。

我对布局设计有点迷失。我已经尝试过

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="-20dp"
        android:visibility="gone" />

    <FrameLayout
        android:id="@+id/fragment2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="-20dp"
        android:visibility="gone" />

</RelativeLayout> 

我甚至同时展示它们两个。每个Fragment使用不同的FragmentTransaction。但是,使用android:layout_above无法实现。

开发这个设计的最简单方法是什么?

2个回答

0

试试这个

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="-100dp"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="50dp"
           android:background="@color/colorAccent"
            />

        <FrameLayout
            android:id="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/colorPrimary"
            />

    </LinearLayout>

</RelativeLayout>

给linearLayout添加动画效果。根据您的要求更改framelayout的可见性(隐藏/显示)


0

也许在FragmentA中使用两个FrameLayout可以帮助处理这两种情况。

  1. 当它是唯一可见的时,您可以使用android:layout_alignParentBottom="true"

  2. 当它位于持有FragmentB的FrameLayout之上时,您可以使用android:layout_above="@+id/fragment2"

然后根据您的情况调整这些的“可见性”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接