【问题标题】:How to show fragment privew in FrameLayout如何在 FrameLayout 中显示片段预览
【发布时间】:2019-05-05 21:39:28
【问题描述】:

我想在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

现在一切正常。 但我认为有更好的方法。

【问题讨论】:

    标签: android


    【解决方案1】:

    你可能想要the tools:showIn attribute

    所以如果你有你的main_frame.xml 那个&lt;include&gt;s 你的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>
    

    希望有帮助!

    【讨论】:

    • 不,我不需要它,我只想在 android studio 中显示预览。类似于include 中的tools:layout=""TextView 中的tools:text="test"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多