Android - 框架布局高度与协调器布局不匹配 [英] Android - Frame layout height not matching with coordinator layout
问题描述
我的 FrameLayout(抽屉式布局中的容器)有问题.FrameLayout 的高度超过了屏幕高度(在底部 android 默认菜单按钮下方).
解决方案 我的第一次尝试是在 FrameLayout 中设置 android:layout_marginBottom="?attr/actionBarSize"
.这解决了在没有垂直可滚动内容的情况下具有固定"高度的不可滚动视图的解决方案(就像通常的具有 match_parent
高度的RelativeLayout).将组件与父底部对齐 (android:layout_alignParentBottom="true"
) 会导致元素仍然可见.在 Android Studio 的预览器中,高度不可见.
然而,这个 marginBotton 修复为根视图是可滚动的片段(如 RecyclerView)引入了一个新问题.对于这些视图,向下滚动时,底部边距将在白色条中可见(如果白色是背景色).这接缝合理,对于那些视图,嵌套滚动功能将滑出工具栏
tl;dr 我通过将 ?attr/actionBarSize
作为底部边距应用于 Framelayout 中显示的不可滚动片段来解决该问题.在此之前,我将工具栏的高度设置为 ?attr/actionBarSize
.
活动布局:
<android.support.v7.widget.Toolbarandroid:id="@+id/工具栏"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"app:layout_scrollFlags="scroll|enterAlways"/></android.support.design.widget.AppBarLayout><框架布局android:id="@+id/容器"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"机器人:方向=垂直"><!-- 这里还有更多的东西--><TextView android:id="@+id/label"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"/></LinearLayout>
我现在面临的唯一缺点是在创建片段布局时 Android Studio 的预览器中会显示空白.
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>
解决方案 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.
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 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
.
Activity layout:
<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>
Fragment layout:
<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>
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 - 框架布局高度与协调器布局不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!