【问题标题】:Android, TabLayout miscalculates height, won't show bottom of layoutAndroid,TabLayout 计算错误高度,不会显示布局底部
【发布时间】:2016-02-01 22:07:22
【问题描述】:

我正在我的应用程序中处理选项卡活动,但选项卡似乎使用了错误的高度(认为布局比任何设备上的布局高约半英寸),因此任何垂直居中在低端关闭,并且我的滚动视图被搞砸了,因为屏幕不会向下滚动足够远以看到视图的底部。我已经尝试在我能想到的任何相关类上修改 layout_height 并隐藏选项卡,但没有任何帮助,我在其他任何地方都找不到这个问题。我在 Android Studio 1.4 中为 TabLayout 活动生成了默认模板。

activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".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:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways">

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

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

</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.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_youtube_searched_for_white_18dp"
    android:visibility="gone"/>

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

fragment_main

<ScrollView 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:background="@color/white"
tools:context="com.example.ggould.flyingfood.HistoryFragment">

<LinearLayout
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

//other stuff

</LinearLayout

</ScrollView>

【问题讨论】:

  • 对此有任何更新吗?我遇到了同样的问题。
  • 我自己一直在寻找这个问题的答案。我的猜测是,当片段获取其布局参数时,选项卡尚未设置,因此它认为它将具有完整的活动高度减去工具栏。但是随后选项卡栏获得了该高度的一部分,并且 viewpager 只是将片段向下滚动了一点(这就是 app:layout_behavior="@string/appbar_scrolling_view_behavior" 行似乎所做的)。不过我还没想好怎么解决。

标签: android view scroll tabs height


【解决方案1】:
 <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:layout_scrollFlags="scroll|enterAlways">

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

您必须为 widget.toolbar 包装内容

【讨论】:

  • 试试这个,可能有用,schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android :layout_height="wrap_content" android:background="@color/white" tools:context="com.example.ggould.flyingfood.HistoryFragment"> //其他东西
  • 不知道你在这里建议什么。如果只是为了让滚动视图 wrap_content 那么我已经尝试过了,它并没有改变任何东西
  • 你定义了方向吗?如果没有,那就试试吧。
【解决方案2】:

在activity_main 上你可以创建一个RelativeLayout。将您的 ViewPager 和 AppBarLayout 放入 RelativeLayout 并像这样设置您的 ViewPager:

<android.support.v4.view.ViewPager
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_below="@+id/appbar"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多