ActionBar导航标签 - 标签的主要活动布局?

时间:2014-08-10 14:52:58

标签: android android-layout android-fragments tabs

我在使用导航标签时遇到很大问题。我知道这个指南http://developer.android.com/guide/topics/ui/actionbar.html#Tabs 但我在理解这篇文章时遇到了问题。它说:

  

首先,您的布局必须包含一个ViewGroup,您可以在其中放置与选项卡关联的每个片段。确保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="blockDescendants"/>
</FrameLayout>

为什么我看不到任何东西?


解决了:这是因为我将高度设置为0dp。

1 个答案:

答案 0 :(得分:0)

请通过以下方式更新您的代码:

 <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=match_parent"
                android:layout:marginTop="20dp"
                android:descendantFocusability="blockDescendants"/>
        </FrameLayout>
相关问题