我在Activity中的CoordinatorLayout(来自最新版本的Design Library)中使用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>
Run Code Online (Sandbox Code Playgroud)
但是在一个以FrameLayout作为根视图的片段中,我需要一个固定在底部的按钮,但它似乎是在屏幕外绘制的.为了能够看到它,我需要添加一个底部填充等于工具栏的高度.
<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>
Run Code Online (Sandbox Code Playgroud)
同样,在元素上设置为"center"的layout_gravity似乎不在此片段的可见区域的中心.
我的理解是CoordinatorLayout只是用于处理滚动内容,这是正确的吗?因此,对于ViewPager片段,只使用常规ViewGroup(如FrameLayout,RelativeLayout,LinearLayout)将其底部部分绘制出屏幕外?
在这种情况下,我是否需要从此片段布局中删除此按钮并将其移动到包含CoordinatorLayout的活动布局?它只需要在第一个片段上显示.
android android-viewpager android-design-library android-coordinatorlayout
我的活动布局如下所示:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
viewpager 有两个片段,一个带有 RecyclerView,另一个带有 LinearLayout。
具有 RecyclerView 的片段按预期工作,并且当滚动 RecyclerView 时操作栏会滚动到屏幕外。
然而,带有 LinearLayout 的另一个片段没有按照我想要的方式显示。LinearLayout 绘制在 TabLayout 下方并延伸到屏幕外。我希望调整它的大小以填充 TabLayout 下方的可用空间,而无需扩展到屏幕外。LinearLayout 看起来像这样:
<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">
<LinearLayout
android:id="@+id/station_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/station_detail_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" />
<include
layout="@layout/station_detail_body"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
<TextView
android:id="@+id/text_view_station_detail_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/text_view_station_detail_empty"
android:visibility="gone" />
</LinearLayout> …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-coordinatorlayout
android ×2