【问题标题】:ActionBar covering my activityActionBar 覆盖我的活动
【发布时间】:2016-06-21 02:45:16
【问题描述】:

我有一个活动,其 xml 为:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="ivykoko.com.likechecker.MainActivity">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/reciclador"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="3dp"
        android:scrollbars="vertical" />


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />


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

它看起来像这样:

如您所见,操作栏覆盖了 recyclerview 的顶部。我能做些什么?

提前致谢。

【问题讨论】:

  • 为什么这会让你大吃一惊?
  • @tim-castelijns 好吧,我是 android 的初学者,不知道该怎么做:S
  • 让我们先告诉我们您希望看到什么,以及为什么
  • 你想做什么?
  • @howdoidothis 我希望操作栏不要覆盖 recyclerview 的第一个元素

标签: android android-actionbar android-recyclerview material-design


【解决方案1】:

将 Appbar 作为协调器布局的直接第一个子元素,并在 RecyclerView 中使用 app:layout_behavior="@string/appbar_scrolling_view_behavior"

【讨论】:

    【解决方案2】:

    app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到您的 RecyclerView。这将导致 CoordinatorLayout 将其定位在 AppBarLayout 下方。

    【讨论】:

      【解决方案3】:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <android.support.design.widget.AppBarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/AppTheme.AppBarOverlay">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:background="?attr/colorPrimary"
                  app:popupTheme="@style/AppTheme.PopupOverlay" />
      
          </android.support.design.widget.AppBarLayout>
      
          <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/reciclador"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="3dp"
              android:scrollbars="vertical" />
      
          <include layout="@layout/content_main" />
      </LinearLayout>
      

      【讨论】:

      • 请添加您更改的内容和原因
      • 那么,我是否必须将我的 CoordinatorLayout 替换为 LinearLayout?感谢您的回答。
      • 只需将我的代码放入您的cordiantor布局中。发生这种情况是因为您在屏幕中放置了一个带有匹配父参数的回收器视图,然后您还放置了一个工具栏,没有任何位置,因此两个组件都在父级顶部对齐,并且工具栏覆盖了回收器视图。
      • 在大多数情况下,CoordinatorLayout 不能替换为LinearLayout,因为屏幕上也有一个FloatingActionButton。下面是 Mahendra 和 SpaceBison 给出的正确答案
      猜你喜欢
      • 1970-01-01
      • 2013-03-25
      • 2022-06-23
      • 2022-10-04
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      相关资源
      最近更新 更多