0

视图如何在 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,它出现在屏幕底部。不知道为什么。

有任何想法吗?

4

1 回答 1

2

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

它看起来像这样:

在此处输入图像描述

于 2015-08-31T09:52:36.853 回答