【问题标题】:Adjust view size inside viewpager, make up for tab height调整viewpager内的视图大小,弥补标签高度
【发布时间】:2015-11-26 17:17:42
【问题描述】:

我在为三个标签中的每一个加载片段的 ViewPager 上遇到了一点问题。在第一个片段内,有一个带有 ListView 的垂直 LinearLayout。列表视图是动态加载项目的,但是当项目计数超过屏幕大小时,它不会滚动以显示其余项目。

我的猜测是片段(设置为匹配父级的高度)被父级布局中的标签视图(DrawerLayout 内的 CoordinatorLayout)下推。如何弥补 ViewPager 中的 TabView 高度或强制片段将其大小限制为剩余屏幕高度?

这是应用的图片:

这是我的代码:

Activity_Home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

        <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="fill_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>

fragment_account.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/accounts_list"
    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="com.mycompany.myapp.fragments.AccountsFragment" />

【问题讨论】:

  • 不,在片段布局中应该只有列表视图及其高度设置为 match_parent。没有额外的线性布局或滚动视图
  • 我更改了片段的 XML 以包含 ListView 作为根元素,但问题仍然存在。仍然在操作栏下方溢出
  • 哦,你的意思是你想让标签和操作栏随着列表视图滚动??
  • 不,看,在上图中,屏幕上还剩下一个列表项。我尽力显示底部滚动分隔符,但大部分都隐藏在导航栏后面
  • 我看到的唯一可能导致问题的是 android:padding in listview,其中有 4 个。

标签: android android-layout android-fragments android-viewpager


【解决方案1】:

我通过从工具栏中删除 app:layout_scrollFlags 属性解决了这个问题。我认为该应用正在等待我滚动以隐藏工具栏,这将使片段缺少高度调整。

【讨论】:

  • 这个答案对我有用!简单有效。真可惜,这行 xml 标记是由 Android Studio 向导自动添加的,并导致我称之为“意外行为”。谢谢你的回答。这是一个很大的帮助。
【解决方案2】:

删除 app:layout_scrollFlags

  <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"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    相关资源
    最近更新 更多