带有片段和底部导航栏的Android FloatingActionButton [英] Android FloatingActionButton with fragment and bottom navigation bar
问题描述
我正在创建具有以下结构的android应用程序:
I'm creating an android app with the following structure:
- 主要活动的底部导航栏在3个不同的片段之间切换
- 其中两个片段将显示项目列表,并带有一个浮动操作按钮(FAB)以添加新项目
- 第三个片段将显示一些不同的东西,不需要FAB.
基于此,FAB似乎应该属于"列表片段,而不是主要活动.
Based on this, it seems like the FAB should "belong" to the list fragments, and not the main activity.
我目前有一个问题,就是FAB隐藏在底部导航栏的后面,例如这个问题一个>.那里有一个很好的解决方案,但这取决于FAB与底部导航栏的布局是否相同.
I am currently have the problem that the FAB hides itself behind the bottom navigation bar, as in this question. There is a nice solution there, but it depends on the the FAB being in the same layout as the bottom navigation bar.
是否有一种方法可以使FAB不将自身隐藏在底部导航后面,而无需将FAB移至主要活动(这会破坏片段的模块化)?
Is there a way to get the FAB to not hide itself behind the bottom navigation, without moving the FAB up to the main activity (which breaks the modularity of the fragments)?
我的活动布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_insetEdge="bottom"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
片段容器"被选定的片段替换,如下所示:
where the "fragment container" is replaced by the selected fragment as below:
TodoListFragment fragment = new TodoListFragment();
//if we were started with an intent, pass on any special arguments
fragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
最后该片段的布局如下:
and finally the fragment has layout as below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:name="..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".TodoListFragment"
tools:listitem="@layout/fragment_todolist_item" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_add_white_24dp"
android:layout_margin="16dp" />
</android.support.design.widget.CoordinatorLayout>
推荐答案
问题在于您的约束.您正在设置FrameLayout的高度以匹配父级.试试这个-
Problem is with your constraints. You are setting FrameLayout's height to match parent. Try this -
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/navigation"/>
这篇关于带有片段和底部导航栏的Android FloatingActionButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!