使用 TabLayout 和 ViewPager 时屏幕下方出现 FloatingActionButton
FloatingActionButton appearing under screen when using TabLayout and ViewPager
看看下面的FAB
:
除非我折叠 Toolbar
,否则它不会出现。这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_alignParentStart="true"
android:layout_below="@+id/toolbar"
android:layout_marginTop="0dp"
android:animateLayoutChanges="true"
android:background="@android:color/white"
android:layoutDirection="locale"
android:orientation="vertical"
android:padding="0dp">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/accent"
android:src="@drawable/ic_add"
app:layout_anchor="@id/list"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
如果我没有为 FAB
设置任何行为,为什么会发生这种情况?是否有任何 属性 我应该添加到任何东西以防止这种行为?
我建议您删除 RelativeLayout 并像这样重组您的布局:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/accent"
android:src="@drawable/ic_add"
app:layout_anchor="@id/list"
app:layout_anchorGravity="bottom|end" />
<com.rey.material.widget.ProgressView
android:id="@+id/progress_bar"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:pv_autostart="true"
app:pv_circular="true"
app:pv_progressMode="indeterminate"
app:pv_progressStyle="@style/Material.Drawable.CircularProgress" />
</android.support.design.widget.CoordinatorLayout>
协调器布局不像相对布局,你不能有多个 children 而不互相干扰。所以只需制作一个 child 协调器布局,即相对布局,并在其中添加 recyclerView 和 floatingActionButton
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/accent"
android:src="@drawable/ic_add"
app:layout_anchor="@id/list"
app:layout_anchorGravity="bottom|end" /></RelativeLayout>
只需从每个 Fragment
中删除您的 FloatingActionButton
,然后将其添加到您的 Activity
。这样,不仅 FAB 会保留在原位,而且还能解决您的问题。然后你可以在你的 Fragment
中使用 getActivity().findViewByIf(id)
并在每个 Fragment
.
中设置一个 onClickListener
看看下面的FAB
:
除非我折叠 Toolbar
,否则它不会出现。这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_alignParentStart="true"
android:layout_below="@+id/toolbar"
android:layout_marginTop="0dp"
android:animateLayoutChanges="true"
android:background="@android:color/white"
android:layoutDirection="locale"
android:orientation="vertical"
android:padding="0dp">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/accent"
android:src="@drawable/ic_add"
app:layout_anchor="@id/list"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
如果我没有为 FAB
设置任何行为,为什么会发生这种情况?是否有任何 属性 我应该添加到任何东西以防止这种行为?
我建议您删除 RelativeLayout 并像这样重组您的布局:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/accent"
android:src="@drawable/ic_add"
app:layout_anchor="@id/list"
app:layout_anchorGravity="bottom|end" />
<com.rey.material.widget.ProgressView
android:id="@+id/progress_bar"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:pv_autostart="true"
app:pv_circular="true"
app:pv_progressMode="indeterminate"
app:pv_progressStyle="@style/Material.Drawable.CircularProgress" />
</android.support.design.widget.CoordinatorLayout>
协调器布局不像相对布局,你不能有多个 children 而不互相干扰。所以只需制作一个 child 协调器布局,即相对布局,并在其中添加 recyclerView 和 floatingActionButton
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/accent"
android:src="@drawable/ic_add"
app:layout_anchor="@id/list"
app:layout_anchorGravity="bottom|end" /></RelativeLayout>
只需从每个 Fragment
中删除您的 FloatingActionButton
,然后将其添加到您的 Activity
。这样,不仅 FAB 会保留在原位,而且还能解决您的问题。然后你可以在你的 Fragment
中使用 getActivity().findViewByIf(id)
并在每个 Fragment
.
onClickListener