How to set a view's height match parent in ConstraintLayout?

后端 未结 4 1695
小蘑菇
小蘑菇 2020-12-22 13:51

I am creating a layout.I want to set my custom toolbar(Which is another layout) to the top of layout and the FrameLayout

相关标签:
4条回答
  • 2020-12-22 14:02

    Note, that unlike other viewgroups in android, constraintlayout should have circular dependencies between the child views. Herewith, you should use 0dp instead of 'match_parent' width and constraints for each side of the child view.

    <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>
    
    0 讨论(0)
  • 2020-12-22 14:03

    give the toolbar an id , and in your frame layout set the width and height to 0dp add the constraints like in this code..

    <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>
    
    0 讨论(0)
  • 2020-12-22 14:08

    Use 0dp that is match_contraint in ConstraintLayout.

    Like android:layout_height="0dp"

    0 讨论(0)
  • 2020-12-22 14:11

    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" />
    

    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.

    0 讨论(0)
提交回复
热议问题