Android add other layout in ConstraintLayout using include tag

前端 未结 3 1349
忘掉有多难
忘掉有多难 2020-12-17 07:37

I make some simple app for test using ConstraintLayout. But I have some problem.

Here is my code

activity_main.xml



        
相关标签:
3条回答
  • 2020-12-17 07:56

    In the constrain layout define the height and width and your constraints as follows:

    <include
        app:layout_constraintTop_toBottomOf="@id/custom_members_toolbar_layout"
        app:layout_constraintBottom_toTopOf="@id/file_xfer_bottom_nav_bar"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        layout="@layout/file_xfer_upload_layout"/>
    

    This will auto resize the height based on your constraints.

    Cheers!

    0 讨论(0)
  • 2020-12-17 07:59

    Not sure if the ideal solution but what worked for me was to have the include tag hosted by its own constraint layout such as

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/todaysFightLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/tvLatestUpdate"
        app:layout_constraintStart_toStartOf="parent">
    
    <include
        layout="@layout/todays_fight_layout" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    And then add constraints from this new constraint layout to the next item to be displayed, in my case a textview with id tvLatestUpdate.

    0 讨论(0)
  • 2020-12-17 08:23

    Android include tag does work with ConstraintLayout, but you need to declare how big is the layout you want to include with the following attributes.

    <include
         layout="@layout/content_main"
         android:layout_width="100dp"
         android:layout_height="250dp"
         .../>
    

    For included layout having dynamic height use wrap_content as a value in layout_height or layout_width attributes of include tag.

    <include
        android:id="@+id/input_include"
        layout="@layout/task_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    After that, your constraints should work.

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