【问题标题】:Fragment with multiple views, "screens"具有多个视图的片段,“屏幕”
【发布时间】:2012-10-22 12:56:01
【问题描述】:

我一直在寻找一种方法来使用带有自己“屏幕”的片段。

我的场景:

  • FragmentActivity -> 布局:带有这些选项卡的选项卡宿主:主页 |网络 |供稿

  • HomeFragment -> 布局:有 2 个按钮,我需要为这些按钮创建一个屏幕,但我不知道该怎么做,也许隐藏正在显示的元素并仅显示我需要的元素“按钮屏幕”

  • NetworkFragment -> 布局:n 个按钮和 n 个屏幕

  • FeedsFragment -> 布局:n 个按钮和 n 个屏幕

标签主机工作正常,我可以在标签(片段)之间切换,但是在片段屏幕上时,我需要单击该按钮并显示片段标签屏幕以外的其他屏幕。

【问题讨论】:

  • 不清楚你在问什么。当您说“显示其他屏幕”时,您是在要求一堆视图吗?
  • 是的@louielouie,就像一个视图堆栈或 java Swing 的卡片布局

标签: android android-layout android-fragments


【解决方案1】:

您要使用的是FrameLayout。这将使您拥有一堆视图。它的部分文档内容如下:

子视图在堆栈中绘制,最近添加的子视图在顶部。

这在 Android 框架中经常用于执行诸如显示空视图或列表视图之类的任务,如下面的代码 sn-p。这个 sn-p 可以显示 ListView 或 TextView。

    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >
    <!-- Here is the list. Since we are using a ListActivity, we
         have to call it "@android:id/list" so ListActivity will
         find it -->
    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"/>

    <!-- Here is the view to show if the list is emtpy -->
    <TextView android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="No items."/>

</FrameLayout>

注意第一个元素在底部,最后一个元素在底部,在膨胀布局之后。在这个特定的示例中,这意味着 TextView 将是唯一可见的,因为它匹配父项(即填充父项。)

如果你想让另一个视图可见,那么你可以让另一个视图不可见:

findViewById(android.R.id.empty).setVisible(View.INVISIBLE);

如果您有多个视图(称为屏幕),只需遍历它们并将您不想显示的视图设置为不可见,以便显示您想要显示的视图。

请注意,如果您想变得更漂亮,可以对片段执行相同的技术。有一篇很好的关于这方面的 Android 培训文章,名为 Building a Flexible UI。它仍然使用 FrameLayout,但它使用 Fragment 事务。但它可能不适用于您的特定情况,因为片段不能包含其他片段,并且您已经依赖 TabHost 中的多个片段,所以从上面更简单的 FrameLayout 方法开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多