[英]How to show fragment privew in FrameLayout
我想在FrameLayout
显示我片段的预览。
事实上我只想在android studio中显示它,而且我不想渲染它。
我的解决方案
在Xml中,我使用include
标签加载home_fragment
预览:
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/nav_view"
android:layout_below="@id/my_toolbar">
<include layout="@layout/home_fragment" />
</FrameLayout>
在onCreate
方法中,我删除了FrameLayout的所有子视图:
FrameLayout frameLayout = findViewById(R.id.main_frame);
frameLayout.removeAllViews(); // remove static preview in activity_main.xml
现在一切正常。 但我认为有更好的方法。
您可能需要这些工具:showIn属性 。
因此,如果你的main_frame.xml
<include>
是你的home_fragment.xml
,那么在home_fragment.xml
只需指向容器布局:
<FrameLayout
tools:showIn="@layout/main_frame"
android:id="@+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Other stuff -->
</FrameLayout>
希望有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.