【发布时间】:2014-08-10 14:52:58
【问题描述】:
让导航选项卡正常工作时遇到了很大的问题。我知道这个指南http://developer.android.com/guide/topics/ui/actionbar.html#Tabs 但我在理解这篇文章时遇到了问题。上面写着:
首先,您的布局必须包含一个 ViewGroup,您可以在其中放置与选项卡关联的每个 Fragment。确保 ViewGroup 具有资源 ID,以便您可以从代码中引用它并交换其中的选项卡。或者,如果选项卡内容将填充活动布局,那么您的活动根本不需要布局(您甚至不需要调用 setContentView())。相反,您可以将每个片段放置在默认的根视图中,您可以使用 android.R.id.content ID 来引用它。
由于我不使用 Fragment 作为我的主布局,但对其他人来说,我认为“替代”方式适合我(我希望 Tab 1 成为我现在拥有的主布局,然后是 2 和3 成为我的片段),但我不知道如何做到这一点。有人可以帮忙吗? 我能够设置选项卡和 TabListener,所以这不是问题。问题是第一个选项卡不应该是片段,而是我的主要活动布局!这是我需要帮助的地方!
我确实尝试将我的主要活动布局用于片段,但由于某种原因,如果我单击该选项卡,选项卡会更改,但屏幕保持白色。我看不到我的片段视图。 旧代码是:
mListView = (ListView) findViewById(R.id.list_view);
mListView.setAdapter(mAdapter);
mListView.setTextFilterEnabled(true);
我在片段中将其更改为:
View view = inflater.inflate(R.layout.list, container, false);
mListView = (ListView) view.findViewById(R.id.list_view);
mListView.setAdapter(mAdapter);
mListView.setTextFilterEnabled(true);
// do stuff here //
return view;
这是 list.xml:
<FrameLayout xmlns:android:="http://schemas.android.com/apk/res/android"
android:id="@id/main_fragment"
android:layout_width="match_parent"
android:layout:height=match_parent">
<ListView android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout:height=0dp"
android:layout_weight="1"
android:layout:marginTop="20dp"
android:descendantFocusability="blocksDescendants"/>
</FrameLayout>
我怎么什么都看不到?
已解决:这是因为我将高度设置为 0dp。
【问题讨论】:
-
您可以使用
ViewPager和Fragments进行基于标签的导航。The problem is that the first tab is NOT supposed to be a fragment, but my main activity layout- 为什么不将此布局用于第一个Fragment? -
我试过了,但由于某种原因,我无法看到我的主布局以这种方式存在的 ListView(我编辑了问题以解释我做了什么)
-
为什么你的布局高度设置为 0dp?删除
weight属性并将高度设置为match_parent -
天哪!这实际上是问题所在。不知道这是怎么回事!非常感谢!
标签: android android-layout android-fragments tabs