如何在 ConstraintLayout 中设置视图的高度匹配父级? [英] How to set a view's height match parent in ConstraintLayout?
问题描述
我正在创建一个 layout
.我想将我的自定义 toolbar
(这是另一种布局)设置到 layout
的顶部和FrameLayout
就在 toolbar
下方.但是 FrameLayout
应该得到 layout
的高度 (match_parent) 的其余部分.如何使用约束布局来实现这一点?
I am creating a layout
.I want to set my custom toolbar
(Which is another layout) to the top of layout
and the FrameLayout
just below the toolbar
. But the FrameLayout
should get rest of the layout
's height (match_parent). How to achieve this using constraint layout ?
推荐答案
解决方案:我假设您将工具栏作为另一个 xml 文件,如下所示:
Solution: I'm assuming that you have your toolbar as another xml file, something like this:
toolbar.xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60dp"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
然后使用 ConstraintLayout
将它与 FrameLayout
一起使用,如下所示:
then using ConstraintLayout
use it with FrameLayout
like this:
<?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">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
</android.support.constraint.ConstraintLayout>
希望这会有所帮助.如果您有任何问题,请发表评论.
Hope this helps. Please comment if you have any issues with it.
这篇关于如何在 ConstraintLayout 中设置视图的高度匹配父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!