在没有 actionBar.newTab() 的情况下实施 Android 选项卡

Implement Android Tabs without actionBar.newTab()

我是Android开发的新手,我正在实现一些基本程序。 我的最低 SDK 是 KitKat。

我想创建一个有多个标签页的应用程序。 我正在使用以下方法:

ActionBar actionBar = getActionBar();
actionBar.newTab().setText("Tab1");
actionBar.newTab().setText("Tab2");

但现在 Android Studio 中已弃用这些方法。

如何根据新方法得到这段代码的准确实现?

请详细说明。

www.developer.android.com对我来说很复杂。

Prior to Android "L" preview, the easiest way to setup tabs with Fragments was to use ActionBar Tabs as described in ActionBar Tabs with Fragments guide. However, all methods related to navigation modes in the ActionBar class (such as setNavigationMode(), addTab(), selectTab(), etc.) are now deprecated.

As a result, tabs are now best implemented by leveraging the ViewPager with a custom "tab indicator" on top. In this guide, we will be implementing the custom tab indicator using the Google recommended method : SlidingTabs Layout

如果您想更详细地了解如何实现它,请查看 this 代码路径教程。

有一种使用第三方库的更简单的方法:PagerSlidingTabStrip

目前,我认为最好用的是 PagerSlidingTabStrip。您可以在创建项目时选择 Android Studio Tabbed Activity 预设(而不是 Blank Activity、Fragment 等)来测试它,看看您是否喜欢它。这是有关如何使用它的教程:
http://www.androidbegin.com/tutorial/android-viewpagertabstrip-fragments-tutorial/

请记住,如果您选择 Android Studio 预设,则必须在 activity_main.xml 内添加 pagertabstrip。像这样:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.PagerTabStrip
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:textColor="#000000" />

如果还有什么问题,尽管问。

如果您想要 android 标签看起来像谷歌更新的应用程序(如 Play 商店)中的标签,那么您将不得不使用 ViewPager 和来自谷歌开源的两个 java 文件 I/O 2014年申请

如果您是初学者,我建议您不要使用 android 开发者网站来获取诸如 android 选项卡之类的常见内容,因为它们只会使它变得过于复杂并且几乎就像计算机科学学位一样,您只使用所学知识的 40%。有数百个教程网站和博客可以向您展示如何以更易于理解的方式做同样的事情 :D