我正在创建一个布局
。我想将自定义的工具栏
(另一个布局)设置在布局
的顶部,并将FrameLayout
放置在工具栏
下面。但是FrameLayout
应该获取布局
剩余的高度(match_parent)。如何使用约束布局实现��一点?
我正在创建一个布局
。我想将自定义的工具栏
(另一个布局)设置在布局
的顶部,并将FrameLayout
放置在工具栏
下面。但是FrameLayout
应该获取布局
剩余的高度(match_parent)。如何使用约束布局实现��一点?
解决方案:我假设您的工具栏是另一个XML文件,类似于以下内容:
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
结合使用,如下所示:
<?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>
希望这可以帮到你。如果你有任何问题,请在评论区留言。
在 ConstraintLayout 中使用 0dp
,它相当于 match_constraint。
例如:android:layout_height="0dp"
android:layout_height="0dp"
,还需要加上 app:layout_constraintBottom_toBottomOf="parent"
这行代码才能解决我的问题。 - Yogesh Katkar给工具栏一个ID,在帧布局中将宽度和高度设置为0dp
,并添加像这段代码中的约束。
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Main2Activity">
<Toolbar
android:id="@+id/toolbar"
app:layout_constraintTop_toTopOf="parent"
android:background="#ab1010"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Toolbar>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#f1f1df"
>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
<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="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<FrameLayout
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>