Android 使用TableLayout和ViewPager时屏幕下出现的浮动操作按钮
请看下面的Android 使用TableLayout和ViewPager时屏幕下出现的浮动操作按钮,android,android-viewpager,android-tablayout,android-coordinatorlayout,floating-action-button,Android,Android Viewpager,Android Tablayout,Android Coordinatorlayout,Floating Action Button,请看下面的FAB: 除非我折叠工具栏,否则它不会出现。以下是我的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_p
FAB
:
除非我折叠工具栏
,否则它不会出现。以下是我的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>
协调器布局的行为与相对布局不同,您不能有多个子项而不相互干扰。因此,只需创建协调器布局的一个子级,即相对布局,并在其中添加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>
只需从每个片段中删除浮动操作按钮
,并将其添加到活动
。这样不仅使晶圆厂保持原位,而且还能解决您的问题。然后,您可以在片段中使用getActivity().findviewbyf(id)
,并在每个片段中设置一个onClickListener
从顶部相对布局android:layou下方=“@+id/toolbar”中删除此行
并将协调器布局设置为最顶层layout@cafebabe1991如果一次没有遇到完全相同的问题,请提供完整的布局代码。如果没有它,解决这个问题是不可能的。单击事件的问题。如何找到当前可见的片段?您可以尝试这种方法。1.2.