无法在NestedScrollView中完全滚动

时间:2018-12-03 17:17:12

标签: android android-layout layout

以某种方式,我无法在NestedScrollView中完全滚动。我的NestedScrollView内部是一个TextView,上面有很多文本,下面有一个按钮。向下滚动时,我只能看到按钮的顶部(只有几个像素)。它滚动得不够远。

这是我的布局:

<?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:id="@+id/appbar"
        android:layout_width="match_parent"
        android:stateListAnimator="@drawable/appbar_always_elevated"
        android:layout_height="128dp">

    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/app_name"
                app:layout_collapseMode="pin">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


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

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
                android:id="@+id/textView"
                style="@style/Text.Body1.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:text="@string/large_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/appbar"/>

        <Button
                android:id="@+id/continueButton"
                style="@style/Widget.Button.Green"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:layout_marginBottom="@dimen/margin"
                android:text="@string/continue_text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"/>
    </LinearLayout>
   </android.support.v4.widget.NestedScrollView>

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

6 个答案:

答案 0 :(得分:0)

LinearLayoutNestedScrollView的高度更改为wrap_content

NestedScrollView及其单个滚动子项背后的想法是,外部(滚动视图)应具有一定的固定/预定高度,而内部 (滚动子级)的动态高度应大于父级。毕竟,如果内容不大于父项,那么滚动的内容是什么?

答案 1 :(得分:0)

您需要在 public static boolean validWord(String s) { int aCounter = 0; int bCounter = 0; char c; for (int i = 0; i < s.length(); i++) { c = s.charAt(i); if ((int) c == (int) 'a') { aCounter++; } else { bCounter++; } } return (aCounter % 2 == 1 && bCounter == 1); } android:fitsSystemWindows="true" CoordinatorLayout 中添加AppBarLayout

详细了解 CollapsingToolbarLayout

Why would I want to fitsSystemWindows?

尝试一下

fitsSystemWindows

答案 2 :(得分:0)

这是我的.xml代码。

请应用。

strings.xml

<string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>

main_activity.xml文件

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adView">

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

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <!--app:contentScrim="?attr/colorPrimary"-->


                <RelativeLayout
                    android:id="@+id/lay"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/inhouse_ad_card_height"
                    android:background="@drawable/app_gradient_square"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:visibility="visible"
                    app:layout_collapseMode="parallax"
                    app:layout_scrollFlags="scroll|enterAlways">

                <!--write tag here-->

                </RelativeLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@drawable/bg_transition"
                    android:visibility="visible"
                    app:contentInsetEnd="@dimen/dim_10"
                    app:contentInsetStart="@dimen/dim_10"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                    <RelativeLayout
                        android:id="@+id/layBtns"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center">

                        <ImageView
                            android:id="@+id/btnSetting"
                            android:layout_width="@dimen/btn_height"
                            android:layout_height="@dimen/btn_height"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_centerVertical="true"
                            android:gravity="center"
                            ads:srcCompat="@drawable/ic_editor_settings" />

                        <TextView
                            android:id="@+id/txtAppTitle"
                            style="@style/TitleFont"
                            android:layout_width="wrap_content"
                            android:layout_height="@dimen/img_btn_height"
                            android:layout_centerInParent="true"
                            android:gravity="center"
                            android:text="@string/display_name"
                            android:textColor="@color/white"
                            android:textSize="@dimen/font_size_extra_large"
                            android:visibility="gone" />

                    </RelativeLayout>

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

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

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

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="true"
                android:orientation="vertical">

                    <!--write tag here-->

            </LinearLayout>

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

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

答案 3 :(得分:0)

只需将您的NestedScrollView更改为此:

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

答案 4 :(得分:0)

尝试一下

   private void updateListViewHeight(ListView myListView) {

    ListAdapter myListAdapter = myListView.getAdapter();

    if (myListAdapter == null) {
        return;
    }
    //get ListView height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount();
    for (int size = 0; size < adapterCount; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //Change Height of ListView
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(params);

}

通过上述方法传递您的列表视图。

答案 5 :(得分:0)

供大家参考,因为我也遇到同样的问题。这是一个已知问题,已在com.google.android.material:material:1.3.0-alpha04

中修复。

https://github.com/material-components/material-components-android/commit/a21a30026a33fc20cf7ad699d32d1298b84096c6