How to switch the layout of include tags in Android database

Asked 2 years ago, Updated 2 years ago, 41 views

For example, I want to realize something like this.
What should I do?

//res/layouts/main.xml
<layout>
  <data>
    <variable
      name = "editable"
      type = "boolean" / >
  </data>

  <FrameLayout>
    <include layout="@{editable?@layout/editform:@layout/show}"/><!--←This doesn't work, I want to make this happen -->
  </FrameLayout>
</layout>

android

2022-09-30 17:38

2 Answers

Instead of describing it in xml, get the parent view from the program and inflate it by conditional branching.


2022-09-30 17:38

Wrap the include tag in the appropriate ViewGroup and switch the visibility of the ViewGroup.

//res/layouts/main.xml
<layout>
  <data>
    <variable
      name = "editable"
      type = "boolean" / >
  </data>

  <FrameLayout android:id="@+id/rootLayout">
    <FrameLayout
      android: id="@+id/editform_layout"
      android:visibility="@{editable?View.VISIBLE:View.GONE}"      
      tools:visibility="View.VISIBLE">
      <include layout="@layout/editform"/>
    </FrameLayout>
    <FrameLayout
      android: id="@+id/show_layout"
      android:visibility="@{editable?View.GONE:View.VISIBLE}"      
      tools:visibility="View.GONE">
      <include layout="@layout/show}"/>
    </FrameLayout>
  </FrameLayout>
</layout>

When I tried it, if the parent Layout (rootLayout) was ConstraintLayout, it collapsed.LinearLayout or FrameLayout would have been fine.


2022-09-30 17:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.