【问题标题】:Using ViewStub with Tabs in Android Layout在 Android 布局中使用带有选项卡的 ViewStub
【发布时间】:2023-03-12 05:42:01
【问题描述】:

我已经阅读了http://developer.android.com/resources/articles/layout-tricks-stubs.html,它解释了如何使用 viewtub 作为布局的惰性包含。

这在我拥有的简单列表视图上运行良好,但是当我尝试在我的实际选项卡式布局中使用它时,一旦选项卡获得任何真实内容,它就会消失。

我已将所有选项卡和选项卡主机设置为使用 wrap_content 作为高度(以免将存根推开)

有没有人使用带有标签视图的存根?有什么诀窍?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <LinearLayout android:orientation="vertical"
   android:layout_width="fill_parent" android:layout_height="wrap_content">
   <TabWidget android:id="@android:id/tabs"
    android:layout_width="fill_parent" android:layout_height="wrap_content" />
   <FrameLayout android:id="@android:id/tabcontent"
    android:layout_width="fill_parent" android:layout_height="wrap_content">
    <ListView android:id="@+id/discqueue" android:layout_width="fill_parent"
     android:layout_height="wrap_content" />
    <ListView android:id="@+id/instantqueue"
     android:layout_width="fill_parent" android:layout_height="wrap_content" />
   </FrameLayout>
   <ViewStub android:id="@+id/stub_navigate"
    android:inflatedId="@+id/panel_navigate" android:layout="@layout/pagination_bar"

    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_gravity="bottom" />
  </LinearLayout>
 </TabHost>
</LinearLayout>

【问题讨论】:

  • 也许这也与列表有关 - 如果我将项目数限制为 5 个左右,导航栏仍然存在,一旦超过它就会被推开。

标签: android layout stub


【解决方案1】:

想通了!

他们在开发者网站上没有提到的技巧是,对于列表/网格类型的动态视图,您实际上希望将现有视图与任何存根合并,而不是像这样将存根插入到您的视图中;

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<include android:id="@+id/cell3" layout="@layout/queue_man" android:layout_height="40dp" android:layout_gravity="bottom" />

    <ViewStub
        android:id="@+id/stub_navigation"
        android:inflatedId="@+id/panel_navigation"

        android:layout="@layout/pagination_bar"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />

</merge>

这当然会让纸杯蛋糕和甜甜圈的用户望尘莫及。

【讨论】:

  • 你能解释一下吗?
  • Sweety - 我的答案中的代码将我问题中的原始布局与新布局中定义的新视图(viewstub)合并。所以 2 个文件(仅包含内部内容的选项卡式视图)和合并文件(将存根引入选项卡下方)。在您的代码中,您调用合并的布局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多