如何使用 ViewPager 使工具栏可见?
How to make visible Toolbar with ViewPager?
我正在阅读 "The Big Nerd Ranch Guide" 第 11 章,我正在编写与 Material 设计非常相似的应用程序。应用程序有两个活动 - 列表和此列表中的项目 detalization。在 detalization 中,activity 是在 onCreate
方法中使用以下代码实现的 ViewPager
:
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);
我怎么理解 setContentView
我们不使用 XML
标记,所以我丢失了 Toolbar
。
本书下一章的应用有ActionBar
。我怎样才能 return 我的 Toolbar
去详细化 activity?
您丢失了 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>
我正在阅读 "The Big Nerd Ranch Guide" 第 11 章,我正在编写与 Material 设计非常相似的应用程序。应用程序有两个活动 - 列表和此列表中的项目 detalization。在 detalization 中,activity 是在 onCreate
方法中使用以下代码实现的 ViewPager
:
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);
我怎么理解 setContentView
我们不使用 XML
标记,所以我丢失了 Toolbar
。
本书下一章的应用有ActionBar
。我怎样才能 return 我的 Toolbar
去详细化 activity?
您丢失了 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>