在anko DSL中相当于<fragment>标签

如何用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> 

安科版本:

 UI { linearLayout { orientation= VERTICAL fragment { // error! name = "com.myapp.MyFragment" } }.lparams(width=..., height=...) } 

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

谢谢!

目前在Anko中没有特别的功能,你可以使用Android API来做到这一点。

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

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