Android Viewpager变得太高

Android Viewpager变得太高,android,android-studio,layout,tabs,android-viewpager,Android,Android Studio,Layout,Tabs,Android Viewpager,我正在设置一个选项卡式Android应用程序。为此,我从androidstudio创建的活动开始,类型为Tabbed activity,样式设置为Action Bar Tabs(带有ViewPager) 生成的主活动有一个协调布局,其中包含AppBarLayout和ViewPager(浮动按钮在这种情况下不感兴趣) 由于ViewPager的高度为match\u parent,我想知道它是否会超出屏幕底部。为了测试它,我在ViewPager中使用的片段中添加了一个TextView,并将其与底部对齐

我正在设置一个选项卡式Android应用程序。为此,我从androidstudio创建的活动开始,类型为Tabbed activity,样式设置为Action Bar Tabs(带有ViewPager)

生成的主活动有一个
协调布局
,其中包含
AppBarLayout
ViewPager
(浮动按钮在这种情况下不感兴趣)

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

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

因此,我的假设是,
ViewPager
与父视图具有相同的高度,但是
AppBar
占据了顶部的空间,因此
ViewPager
向下移动。现在,如何正确设置
ViewPager
的高度,使其占据
AppBar
下的所有剩余空间?高度的唯一可能值是
match\u parent
wrap\u 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>

因此,我的假设是,ViewPager与父级具有相同的高度,但AppBar在顶部占据空间,因此ViewPager向下移动

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

你有几个选择

  • 您可以将底部定位视图移动到视图寻呼机外部,并将其定位到
    坐标布局的底部。这样,无论内容在“查看寻呼机”中的滚动位置如何,它都将始终可见

    执行此操作时,确实需要使用下边距偏移“查看寻呼机”,以便在向上滚动内容时完全可见:

        <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
    ,并在不使用
    AppBarLayout的情况下使用
    工具栏


请查看屏幕截图,因为它是一个
坐标布局
滚动
行为,对于
AppBarLayout
查看页面
将占用相当于
屏幕高度-工具栏高度的空间
,这样当
viewpager
向上滚动时,工具栏将退出屏幕,
viewpager
将完全可见。现在,如果您不希望出现这种行为,而不是使用
coordinatorLayout
,您可以使用
LinearLayout
,但是您的
AppBar
将无法滚动。你会得到你想要的,这很有效。(和希曼殊的建议一样,谢谢)。现在状态栏(电池、天线、消息等)为白色。我会去看看是否能找到解决方案。找到了:不要将
坐标布局
更改为
线性布局
,而是在
坐标布局
中添加一个
线性布局
(请参阅)