Viewpager变得太高了

时间:2016-08-23 10:07:10

标签: android android-studio layout tabs android-viewpager

我正在设置一个标签式Android应用。为此,我开始使用Android Studio创建的活动选项卡式活动,其样式设置为操作栏选项卡(使用ViewPager)

生成的主要活动有一个CoordinatorLayout,其中包含AppBarLayoutViewPager(浮动按钮对于这种情况不感兴趣)。

由于ViewPager的高度为match_parent,我想知道它是否会超出屏幕底部。为了测试它,我在TextView内部使用的片段中添加了ViewPager并将其与底部对齐。

运行应用时,我看不到额外的TextView。将ViewPager的高度更改为400dp后,TextView变为可见。

所以我的假设是ViewPager获得与父级相同的高度,但AppBar占据了顶部的空间,因此ViewPager向下移动。现在应该如何正确设置ViewPager的高度,以便它占用AppBar下的所有剩余空间?唯一可能的高度值是match_parentwrap_content和固定值。

我希望有一个“仅限xml”的解决方案。

为了完整性,这里是主要活动的布局

<?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"
                                             xmlns:tools="http://schemas.android.com/tools"
                                             android:id="@+id/main_content"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent"
                                             android:fitsSystemWindows="true"
                                             tools:context="nl.ravelin.so_tabs.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>

和主要片段

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="nl.ravelin.so_tabs.MainActivity$PlaceholderFragment">

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="How low can you go."/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

  

所以我的假设是ViewPager获得与父级相同的高度,但AppBar占据了顶部的空间,因此ViewPager向下移动。

你是完全正确的。这对我来说也有点意外。

你有几个选择。

  • 您可以将底部锚定视图移动到视图寻呼机外部,并将其锚定到CoordinatorLayout的底部。这样,无论视图寻呼机内部的滚动位置如何,它都将始终可见。

    执行此操作时,您需要使用下边距偏移视图寻呼机,以便在内容向上滚动时,它完全可见:

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="?attr/actionBarSize"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="How low can you go."/>
    

    这样做的缺点是内容不随视图寻呼机移动。我使用此方法将按钮锚定到适用于整个屏幕的底部。

  • 如果该内容绝对需要一直显示并分页,那么滚动的应用栏可能不适合您的UI。将CoordinatorLayout更改为LinearLayout并使用Toolbar而不使用AppBarLayout

相关问题