【发布时间】: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