基本的ConstraintLayout布局(工具栏、片段容器和底部导航栏)

5

我将适应Kotlin和ConstraintLayouts技术来开发Android应用程序。 我试图创建一个简单的布局,包含工具栏和底部导航视图。

这是我当前的xml代码:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="@dimen/toolbar_title_text_size"
            android:textStyle="bold" />

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/menu_main" />

</android.support.constraint.ConstraintLayout>

很不幸,我没有理解清楚某些东西,看起来无法让片段容器填充工具栏和底部导航之间的空间。

有人能指点一下正确方向吗?


抱歉!我正在用Ruby on Rails重写它,花了很长时间来实现功能平衡...也打算迁移到AWS或Azure堆栈 :) 谢谢你提醒我更新我的网站链接到我的github! - isuPatches
同样的祝福送给你,我的朋友! - isuPatches
2个回答

16

您的片段容器中高度需要为0dp,这相当于“MATCH_CONSTRAINT”。

android:layout_height="0dp"

您可以在此处查找ConstraintLayout

它应该是这样的。

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/colorAccent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"/>

叹气 谢谢...我知道这只是一些简单而微小的东西。 - isuPatches
1
我简直不敢相信我在那上面浪费了一个小时...太傻了 - isuPatches
解决方案:android:layout_height="0dp" 为您节省了时间! - InsaneCat
谢谢,将match_parent更改为0dp已经生效。 - Plugie

-1

这适用于我正在使用的布局。

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="@android:color/white"
        android:textSize="30dp"
        android:textStyle="bold" />

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

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottom_navigation"/>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/buttom_navigation"
    android:background="?android:attr/windowBackground"/>

</android.support.constraint.ConstraintLayout>

附上带有答案的屏幕截图。

Screenshot of the sample


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接