问题描述
我的FrameLayout(抽屉式布局中的容器)有问题. FrameLayout的高度超过了屏幕高度(位于底部的android默认菜单按钮下方).
I have a problem with my FrameLayout (Container in Drawer Layout). The height of the FrameLayout exceeds the screen height (below the android default menu buttons at bottom).
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/navContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
推荐答案
我的第一次尝试是在FrameLayout上设置android:layout_marginBottom="?attr/actionBarSize"
.这解决了对于没有垂直滚动内容的固定"高度的非滚动视图的解决方案(例如具有match_parent
高度的常规RelativeLayout).将组件与父级底部(android:layout_alignParentBottom="true"
)对齐将生成一个仍可见的元素.在Android Studio的预览器中,看不到高度超过了.
My first attempt was to set a android:layout_marginBottom="?attr/actionBarSize"
at the FrameLayout. This solved the solution for non-scrollable views having a "fixed" height in terms of no vertically scrollable content (like a usual RelativeLayout with match_parent
height). Aligning a component to the parent bottom (android:layout_alignParentBottom="true"
) results in a still visible element. In Android Studio's Previewer the no exceeding of the height is visible.
但是,此marginBotton修补程序为其根视图可滚动的片段(如RecyclerView)引入了一个新问题.对于这些视图,向下滚动时,底部边距将在白色栏中显示(如果白色是背景色).这种接缝合理,因为对于那些视图,嵌套滚动功能将滑出工具栏
However, this marginBotton-fix introduces a new problem for fragments whose root view is scrollable (like a RecyclerView). For these views when scrolling down the bottom margin will become visible in a white bar (in case white is the background color). This seams reasonable, as for those views the nested scrolling feature will slide out the toolbar
tl; dr 我通过将?attr/actionBarSize
作为底部边距应用于在Framelayout中显示的不可滚动片段来解决此问题.在此之前,我将工具栏的高度设置为?attr/actionBarSize
.
tl;dr I worked around that issue by applying the the ?attr/actionBarSize
as bottom margin to non-scrollable fragments that are shown inside the Framelayout. Prior to that I set the height of the toolbar to be ?attr/actionBarSize
.
活动布局:
<android.support.design.widget.AppBarLayout
android:id="@+id/navContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
片段布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize"
android:orientation="vertical">
<!-- Further stuff here -->
<TextView android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</LinearLayout>
我现在面临的唯一不足是创建片段布局时要在Android Studio的预览器中显示的空白.
The only downside I faced right now is the white space to be shown in Android Studio's Previewer while creating the fragment layout.
这篇关于Android-框架布局高度与协调器布局不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!