【问题标题】:How does layout work in a CoordinatorLayout?布局如何在 CoordinatorLayout 中工作?
【发布时间】:2015-08-31 09:22:57
【问题描述】:

视图如何在 CoordinatorLayout 中定位?比如……

<?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"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

   <android.support.design.widget.AppBarLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
       <android.support.v7.widget.Toolbar
           android:id="@+id/toolbar"
           android:layout_width="match_parent"
           android:layout_height="?attr/actionBarSize"
           app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>
   </android.support.design.widget.AppBarLayout>

   <android.support.v4.widget.NestedScrollView
       android:id="@+id/scroll"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@android:color/holo_blue_light"
       android:fitsSystemWindows="true"
       app:layout_behavior="@string/appbar_scrolling_view_behavior">
       <TextView
           android:id="@+id/description"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="@string/hello_world" />
   </android.support.v4.widget.NestedScrollView>    
</android.support.design.widget.CoordinatorLayout>

如果我将其子文本视图填充为 1000 行,那么它会显示正确,并且文本将从 appbarlayout 下方开始。

但是,由于它只显示 hello world,它显示在屏幕底部。 不知道为什么。

有什么想法吗?

【问题讨论】:

  • 请发截图
  • 您可以尝试将 android:layout_gravity="fill_vertical" 添加到您的nestedScrollView。有人报告它是 22.2.0 中的一个错误,也许更新也可能有所帮助。
  • 正确的@hars 有帮助。非常感谢

标签: android coordinator-layout


【解决方案1】:

因为 NestedScrollView 的高度是 android:layout_height="wrap_content" 所以它在底部显示内容。您只需将 NestedScrollView 的高度更改为 android:layout_height="match_parent",如下所示:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    </android.support.v4.widget.NestedScrollView>

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

看起来像这样:

【讨论】:

  • 糟糕,我错过了!但是,如果您尝试滚动“hello world”,工具栏会按原样消失,但 hello world 会移动到屏幕底部。
  • @aidanmack 如果对您有帮助,请作为正确答案。
  • 还需要将 android:layout_gravity="fill_vertical" 添加到 Nest 滚动视图中,以阻止它出现在滚动排序的底部。谢谢大家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多