【问题标题】:cant position FrameLayout inside ConstraintLayout无法在 ConstraintLayout 内定位 FrameLayout
【发布时间】:2017-12-28 17:03:15
【问题描述】:

构建应具有的主应用布局:

1) ConstraintLayout 作为根 - 因为它是 RelativeLayout 的新趋势变化

2) AppBarLayout 里面有工具栏

3) 底部的BottomNavigationView

4) BNV 右下角的 FAB

5) 和 FrameLayout - 我的片段的容器,必须在 AppBarLayout 和 BottomNavigationView 之间。

我无法定位我的 FrameLayout。它仅显示具有完整设备大小或 0dp 的 match_parent。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_constraintTop_toTopOf="@+id/frame_container">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">

        </android.support.v7.widget.Toolbar>

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

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:clickable="true"
        android:focusable="true"
        app:layout_constraintBottom_toTopOf="@+id/navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_home_black_24dp" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/primary"
        app:elevation="8dp"
        app:itemIconTint="@color/icons"
        app:itemTextColor="@color/icons"
        app:layout_constraintBottom_toBottomOf="@+id/frame_container"
        app:layout_insetEdge="bottom"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android xml android-constraintlayout


    【解决方案1】:

    像这样设置 constraintTop 和 constraintBottom:

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintTop_toBottomOf="@+id/appbar"
        app:layout_constraintBottom_toTopOf="@+id/navigation">
    

    并且还要编辑其他Views的约束

    【讨论】:

    • 为什么 height = 0dp ?会扩大吗?
    • 0dp 表示它将占据 appbar 和导航之间的整个空间
    • 0dp 表示会匹配约束,这种情况下会占用constraintTop和constraintBottom之间的空间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多