【问题标题】:CoordinatorLayout + ActionBar + FragmentsCoordinatorLayout + ActionBar + Fragments
【发布时间】:2015-12-04 13:25:54
【问题描述】:

当我尝试使用CoordinatorLayout + AppBarLayout 放置片段时遇到问题。

我尝试使用属性app:layout_behavior="@string/appbar_scrolling_view_behavior" 将不同的片段加载到 ActionBar 下方的 RelativeLayout 内容中,但是当我在屏幕底部加载带有两个按钮的片段时,这些片段会加载到屏幕外。

加载片段的内容超出屏幕并且内容总是超出底部。

这是我main_activity.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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".views.activities.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        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"
            app:layout_scrollFlags="scroll|enterAlways" /> <!-- Indica como afecta a los hijos de AppBar -->

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

    <!-- Fragment are loaded here -->
    <RelativeLayout
        android:id="@+id/containerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" > 
    </RelativeLayout>

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

这是内容在屏幕外的屏幕截图。

这是我遇到问题的片段的屏幕截图:

这是在模拟器中加载片段的屏幕截图。您可以看到底部按钮没有出现。它们被导航栏隐藏:

我怎样才能避免这个问题?

编辑

<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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".views.activities.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        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"
            app:layout_scrollFlags="scroll|enterAlways" /> <!-- Indica como afecta a los hijos de AppBar -->

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

    <!-- Main content -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
           android:id="@+id/containerLayout"
           android:layout_width="match_parent"
           android:layout_height="match_parent">

        </FrameLayout>

    </RelativeLayout>

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

编辑 2:我尝试将 ToolBar 放入 RelativeLayout,但它只能部分工作。现在滚动到 recyclerView 时,actionBar 上的动画不起作用

<?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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".views.activities.HomeActivity">

    <!-- Main content -->
    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" > 

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

            <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"
                app:layout_scrollFlags="scroll|enterAlways" /> <!-- Indica como afecta a los hijos de AppBar -->

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

        <FrameLayout
            android:id="@+id/containerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/appBarLayout">

        </FrameLayout>

    </RelativeLayout>

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

【问题讨论】:

  • 尝试在您的RelativeLayout 中添加一个FrameLayout,然后在其中添加您的Fragments
  • 我会尝试你的解决方案,我现在评论你。谢谢:)
  • @Amy 你的解决方案不起作用:(
  • 请用您的更改更新您的代码。
  • 我编辑了我的帖子@Amy。感谢您的帮助:)

标签: android android-fragments android-coordinatorlayout android-appbarlayout android-support-design


【解决方案1】:

我终于发现CoordinatorLayout 发生的情况是AppBarLayout 下方的视图的高度大小就好像Toolbar 已经滚动到了一边。然后,当工具栏 处于正常位置时,视图会被简单地向下推。当 CoordinatorLayout 向上滚动所有内容时,它不会调整大小。

这意味着任何附加到该视图底部的视图都将被滚动到一边,要么不可见,要么部分可见。

那么你如何解决这个问题?你需要做两件事:

  1. AppBarLayoutToolbar 带入您的片段。该片段可以在Toolbar 上设置支持ActionBar 并执行通常在活动中完成的所有操作。所以现在你的活动布局可以像这样简单:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/containerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".views.activities.HomeActivity"/>
    
  2. 将按钮附加到CoordinatorLayout 的底部。 CoordinatorLayoutFrameLayout 的子类,因此您可以在子视图中使用 layout_gravity="bottom"。所以现在您的片段 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:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".views.activities.HomeActivity">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            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"
                app:layout_scrollFlags="scroll|enterAlways" /> <!-- Indica como afecta a los hijos de AppBar -->
    
        </android.support.design.widget.AppBarLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" > 
    
            <!-- 
              rest of your fragment layout goes here 
              it needs to have a scrolling component
              -->
    
            <!-- 
              you *might* need a spacer in order to see
              the bottom of your view over the top of
              the buttons
              -->
            <Space
                android:layout_width="match_parent"
                android:layout_height="48dp" />
            <!-- or you could put a bottom margin on your layout -->
    
        </RelativeLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_gravity="bottom">
    
            <!-- your buttons go here -->
    
        </LinearLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

【讨论】:

  • 对我有用的唯一方法是添加 android:paddingBottom="?attr/actionBarSize"
  • 我将 android:paddingBottom="?attr/actionBarSize" 添加到按钮的片段中。因此,对于我将项目放在屏幕底部的每个片段:(
猜你喜欢
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
相关资源
最近更新 更多