<fragment> anko DSL 中的等效标签

<fragment> tag equivalent in anko DSL

如何用等效的 Anko DSL 替换这个 XML?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

      // more code here....

      <fragment android:id="@+id/my_fragment"
          android:layout_width="340dp"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:name="com.myapp.MyFragment"
          />

      // more code here....

</LinearLayout>

Anko 版本:

UI {
    linearLayout {
        orientation= VERTICAL

        fragment {  // error! 
             name = "com.myapp.MyFragment"
        }

    }.lparams(width=..., height=...)
}

似乎在 DSL 中没有片段标签的 eqiuvalen。

谢谢!

目前 Anko 中没有特殊功能,您可以使用 Android API 来完成此操作。

supportFragmentManager.beginTransaction().add(this.id,
        com.myapp.MyFragment()).commit()

请注意,您需要为 linearLayout 设置 id,因为 add 方法需要设置它。