【问题标题】:FAB inside FrameLayout to be on top of fragmentsFrameLayout 内的 FAB 位于片段之上
【发布时间】:2016-06-03 19:45:33
【问题描述】:

在我的布局中,我有一个FrameLayout,我打算在其中根据用户输入显示不同的布局。我想在所有这些布局中显示FloatingActionButton

为了不重复FloatingActionButton,我想将它附加到FrameLayout。问题是按钮没有正确显示在fragment 布局的顶部。例如,最初显示的fragment,有一个GridView,后面是按钮。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context="xeniasis.mymarket.MainActivity">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="4">

        <LinearLayout
            android:id="@+id/categories_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingRight="20dp">

            <ExpandableListView
                android:id="@+id/categories_listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/mainPane"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/settings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|start"
                android:layout_margin="8dp"
                android:clickable="true"
                android:elevation="5dp"
                android:src="@android:drawable/ic_menu_search" />
        </FrameLayout>
    </LinearLayout>
</LinearLayout>

以及以编程方式显示的fragment 布局:

<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"
    android:orientation="vertical"
    tools:context="xeniasis.mymarket.MainActivity">

        <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swipeRefreshContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingTop="@dimen/activity_vertical_margin">

            <GridView
                android:id="@+id/productsGridview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:numColumns="3"></GridView>

        </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

结果:

【问题讨论】:

  • 你所有的片段都在同一个Activity上吗?如果是这样,您可以将其添加到 Activity 布局中,使其始终位于 FrameLayout 的顶部。
  • 是的,它们都在 MainActivity 中,这是它的布局
  • 你在哪里膨胀片段?在FrameLayoutmainPane?
  • 是的,在我的 MainActivity 中

标签: android android-layout android-fragments android-framelayout floating-action-button


【解决方案1】:

FrameLayout 中,声明另一个Framelayout 并在那里对片段进行膨胀。它应该可以工作。

... <LinearLayout>
     .....

   <FrameLayout
        android:id="@+id/mainPane"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">

        <FrameLayout
            android:id="@+id/fragment_inflate"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_margin="8dp"
            android:clickable="true"
            android:elevation="5dp"
            android:src="@android:drawable/ic_menu_search" />
    </FrameLayout>

【讨论】:

    【解决方案2】:

    这样改,让FAB和fragment不共享同一个FrameLayout:

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:orientation="vertical"
        tools:context="xeniasis.mymarket.MainActivity">
    
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:weightSum="4">
    
            <LinearLayout
                android:id="@+id/categories_layout"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:paddingRight="20dp">
    
                <ExpandableListView
                    android:id="@+id/categories_listView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
    
            <FrameLayout
                android:id="@+id/mainPane"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"/>
    
        </LinearLayout>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_margin="8dp"
            android:clickable="true"
            android:elevation="5dp"
            android:src="@android:drawable/ic_menu_search" />
    
        </FrameLayout>
    </LinearLayout>
    

    【讨论】:

    • 这会将 FAB 放置在屏幕的左下角。我想要它在FrameLayout 的左下角,它只有屏幕的 3/4
    【解决方案3】:

    在框架布局中添加 FAB 而不使用 coordinativelayout

       <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/framelayout"
            android:layout_below="@+id/drawer_toolbar"
            android:layout_above="@+id/drawer_foot" >
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/shareFab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:layout_margin="8dp"
                android:clickable="true"
                android:elevation="5dp"
                android:src="@android:drawable/ic_menu_share" />
        </FrameLayout>
    

    【讨论】:

      【解决方案4】:

      找到您的框架布局并调用此方法并在此方法中传递您的框架布局 100% 它将起作用

          private void addFloatingButton(FrameLayout frameLayout) {
          int i20 = 20;
          int i50 = 50;
          FrameLayout.LayoutParams clFloatingButtonParams = new FrameLayout.LayoutParams(i50, i50);
          clFloatingButtonParams.setMargins(0, 0, i20, i20);
          clFloatingButtonParams.gravity = Gravity.BOTTOM | Gravity.END;
          FloatingActionButton clFloatingActionButton = new FloatingActionButton(getActivity());
          clFloatingActionButton.setId(R.id.calander_view_fab);
                       frameLayout.addView(clFloatingActionButton, clFloatingButtonParams);
      
            }
      

      【讨论】:

        【解决方案5】:

        您可以将 layout_gravity 添加到 FAB,然后您可以将其设置在您想要的任何角落。

        右下角设置:

        android:layout_gravity="right|bottom"
        

        左下角设置:

        android:layout_gravity="left|bottom"
        

        左上角设置:

        android:layout_gravity="left|top"
        

        右上角设置:

        android:layout_gravity="right|top"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-03
          • 1970-01-01
          • 1970-01-01
          • 2013-10-05
          • 2017-03-02
          • 1970-01-01
          • 2016-05-21
          相关资源
          最近更新 更多