【问题标题】:ListView overlaps Collapsing ToolbarListView 重叠折叠工具栏
【发布时间】:2023-03-09 23:27:02
【问题描述】:

我曾经使用 Android Studio 中的 Project-Template 设置一个折叠工具栏,并尝试在 NestedScrollView 中实现一个 ListView(参见下面的代码)

我有最新的 Android Studio 版本,并且 AVD 在 Android 9 上运行

我已经试过了

  1. 更改 NestedScrollView 的 android:fillViewport="true"
  2. 实现我自己的 ListView (Collapsing Toolbar with ListView)
  3. 将 ListView 包装到 LinearLayout 中

我找到了几个使用RecyclerView 的解决方案,我不想使用这种方法 - 如果这是最后一种方法,我会;)。 (这是一个学校项目,我们应该只使用我们在学校学到的组件掌心

我将跳过以下几个 XML 导入行:

<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true"
    tools:context=".MainScreen">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|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:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <fragment
        android:id="@+id/fragment1"
        android:name="net.mountdev.alcohollevel.EntryList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@drawable/ic_add_white_24dp" />

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

嵌套列表视图

<android.support.v4.widget.NestedScrollView 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainScreen"
    tools:showIn="@layout/activity_main_screen">

        <ListView android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"/>

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

目前的结果是,ListView 在 ActionBar 中不应该是(见截图)

【问题讨论】:

    标签: java android android-layout listview


    【解决方案1】:

    问题在于fragment。它占据了整个空间。尝试类似

    <RelativeLayout
        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:fitsSystemWindows="true">
    
    <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:fitsSystemWindows="true">
    
        <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|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:layout_collapseMode="pin"
            />
    
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    
    <fragment
            android:layout_below="@+id/app_bar"
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    
    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_anchor="@id/app_bar"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="25dp"
            android:layout_marginEnd="16dp"
            app:layout_anchorGravity="bottom|end"/>
    

    【讨论】:

      【解决方案2】:

      android.support.design.widget.CoordinatorLayout 更改为RelativeLayout

      在你的AppBarLayout 中使用android:layout_alignParentTop="true"

      为您的fragment 添加android:layout_below="@+id/app_bar"

      【讨论】:

      • 谢谢! ListView 现在位于工具栏下方,但仅显示一个元素 (?)。 floatActionButton 也移到了左上角(如果认为以后可以修复)。
      • 对于浮动按钮添加到它 android:layout_alignParentBottom="true" android:layout_alignParentRight="true" 并且对于可能是因为滚动视图的元素将其更改为其他内容
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多