【问题标题】:Make Toolbar in NestedScrollView not scrollable使 NestedScrollView 中的工具栏不可滚动
【发布时间】:2018-11-02 20:54:50
【问题描述】:

我在NestedScrollView 中有一个自定义Toolbar。 在NestedScrollView中也有一些TextViewsEdittexts

我的问题是不仅TextViewsEdittexts 被滚动,Toolbar 也被滚动。

滚动NestedScrollView 中的Toolbar 是合乎逻辑的。

我想要一个自定义的Toolbar,其顶部有一个绝对位置,并且TextViewsEdittexts 可滚动。

这是我正在使用的代码:

<android.support.v4.widget.NestedScrollView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

 <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#ffffff"
    android:elevation="5dp"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetEnd="50dp"
    app:contentInsetRight="50dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    app:popupTheme="@style/AppTheme.PopupOverlay">

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

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="A long unimportant text"/>

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

所以我尝试只将TextViewsEdittexts 包装在ScrollView 中,例如:

        <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#ffffff"
            android:elevation="5dp"
            android:minHeight="?attr/actionBarSize"
            app:contentInsetEnd="50dp"
            app:contentInsetRight="50dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay">
            </android.support.v7.widget.Toolbar>

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="A long unimportant text"/>

                </ScrollView>

        </LinearLayout>

但它不起作用。

使用自定义 Toolbar 非常很重要。

【问题讨论】:

  • 看看折叠工具栏。?
  • 你没有关闭工具栏
  • @Veener 我只是在问题中忘记了它,但在我的代码中却没有。但无论如何感谢您的提示,我编辑了我的问题。
  • 您的 ScrollView 中是否有多个 TextView?
  • 只是为了确定......在您将 TextView 包装在滚动视图中的第二种方法中,整个布局是包装在 nestedScroll 内还是线性布局是根布局? @雅各布

标签: android scrollview android-toolbar android-nestedscrollview nestedscrollview


【解决方案1】:

尝试相对布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EmptyActivity">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffffff"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetEnd="50dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="50dp"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="A long unimportant text" />

    </ScrollView>

</RelativeLayout>

【讨论】:

  • 正如 Ben P 在他的回答中所说,值得一提的是 ScrollView 必须有一个直接的孩子。因此,如果您有多个 TextView,则需要将其包装在另一种布局中。
【解决方案2】:

ScrollView(和NestedScrollView)将允许您向它们添加多个直接子级,但为了使它们正常工作,它们必须只有一个直接子级。因此,将 ScrollView 的内容包装在 wrap_content LinearLayout 中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="A long unimportant text"/>

            <!-- many more views -->

        </LinearLayout>

    </ScrollView>

</LinearLayout>

【讨论】:

    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多