gpt4 book ai didi

android - Viewpager 变得太高

转载 作者:行者123 更新时间:2023-11-29 17:15:46 38 4
gpt4 key购买 nike

我正在设置一个选项卡式 Android 应用程序。为此,我从 Android Studio 创建的 Tabbed Activity 类型的 Activity 开始,并将样式设置为 Action Bar Tabs (with ViewPager)

生成的主要 Activity 有一个 CoordinatorLayout,包含 AppBarLayoutViewPager( float 按钮对这种情况没有意义)。

由于 ViewPager 的高度为 match_parent,我想知道它是否会超出屏幕底部。为了测试它,我向 ViewPager 中使用的 fragment 添加了一个 TextView 并将其对齐到底部。

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

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

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

为了完整起见,这里是主要 Activity 的布局

<?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>

和主要 fragment

<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>

最佳答案

So my assumption is that the ViewPager gets the same height as the parent, but the AppBar takes up space at the top, so the ViewPager is shifted down.

你完全正确。这也让我有些意外。

这里有几个选项。

  • 您可以将底部锚定 View 移到 View 寻呼机之外,并将其锚定到 CoordinatorLayout 的底部。这样,无论内容在 View 分页器中的滚动位置如何,它都将始终可见。

    执行此操作时,您确实需要用底部边距偏移 View 分页器,以便当内容向上滚动时,它是完全可见的:

        <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."/>

    这样做的缺点是内容不会随着 View 分页器移动。我用它来处理适用于整个屏幕的底部 anchor 按钮。

  • 如果该内容绝对需要始终显示并分页显示,那么滚动应用栏可能不适合您的 UI。将 CoordinatorLayout 更改为 LinearLayout 并在没有 AppBarLayout 的情况下使用 Toolbar

关于android - Viewpager 变得太高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39098185/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com