【问题标题】:How to make visible Toolbar with ViewPager?如何使用 ViewPager 使工具栏可见?
【发布时间】:2016-01-12 01:16:35
【问题描述】:

我正在阅读第 11 章的“The Big Nerd Ranch Guide”,并且正在编写与 Material Design 非常相似的应用程序。应用程序有两个活动 - 列表和此列表中的项目详细化。在 detalization 活动中使用此代码在 onCreate 方法中实现 ViewPager

mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);

我如何理解setContentView 我们不使用XML 标记,所以我丢失了我的Toolbar

在本书的下一章应用有ActionBar。如何在 detalization 活动中返回我的 Toolbar

【问题讨论】:

  • 您应该将 ViewPager 放在您的 xml 布局中,请参见此处:stackoverflow.com/questions/31698756/… 底部的选项卡:stackoverflow.com/questions/32984706/…
  • @DanielNugent 我有特别复杂的架构,其中包含这本书的片段,我正在尝试升级这个应用程序,但我根本不知道它是如何工作的,所以我不明白为什么我有在第二个活动中丢失了我的工具栏、FAB 和导航抽屉。如果您有空闲时间,您可以查看我的存储库并给我更多具体的建议吗? github.com/kostyabak/TeamMaster
  • 您丢失了 TabLayout 和 FAB,因为您只使用 ViewPager 调用 setContentView()。如果你想拥有一个 ViewPager 和一个 TabLayout 和一个 FloatingActionButton,只需在一个拥有这三者的 XML 布局上调用 setContentView()。查看链接的答案以了解实施细节。
  • @DanielNugent 你应该回答 ^ 以便 OP 可以批准它。

标签: android android-layout android-activity android-viewpager android-toolbar


【解决方案1】:

您丢失了 TabLayout 和 FAB,因为您只使用 ViewPager 调用 setContentView()

如果你想拥有一个 ViewPager 和一个 TabLayout 和一个 FloatingActionButton,只需在一个拥有这三者的 XML 布局上调用 setContentView()。像这样的东西会将 TabLayout 放在顶部:

<RelativeLayout
    android:id="@+id/main_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        app:tabMode="fixed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        app:tabTextColor="#d3d3d3"
        app:tabSelectedTextColor="#ffffff"
        app:tabIndicatorColor="#ff00ff"
        android:minHeight="?attr/actionBarSize"
        />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tab_layout"/>

</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-18
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多