我在 Activity 的 CoordinatorLayout(来自最新版本的设计库)中使用 ViewPager。此 ViewPager 的某些片段具有 RecyclerView 或 NestedScrollView 等布局,但有些片段由于内容小而无法滚动。
<android.support.design.widget.AppBarLayout
android:id="@+id/tabanim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/MyTheme">
<android.support.v7.widget.Toolbar
android:id="@+id/tabanim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabanim_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/tabanim_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
但是在一个以 FrameLayout 作为根视图的 Fragment 中,我需要一个固定在底部的按钮,但它似乎是在屏幕外绘制的。为了能够看到它,我需要添加一个等于工具栏高度的底部填充。
<FrameLayout 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:background="@android:color/white">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Home screen"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="bottom"
android:gravity="center"
android:text="@string/brand"
android:textColor="@color/brandColor"
android:textSize="20sp" />
</FrameLayout>
同样,在元素上设置为 'center' 的 layout_gravity 似乎不在此片段的可见区域的中心。
我的理解是 CoordinatorLayout 仅用于处理滚动内容,对吗?因此,对于 ViewPager 片段仅使用常规 ViewGroup(如 FrameLayout、RelativeLayout、LinearLayout)将其底部绘制到屏幕外?
在这种情况下,我是否需要从此片段布局中删除此按钮并将其移动到包含 CoordinatorLayout 的活动布局?它只需要在第一个片段上显示。