Android的动作条导航标签的外观在手机和平​​板电脑的不同 [英] Android ActionBar Navigation Tabs looks differ in phones and tablets

查看:150
本文介绍了Android的动作条导航标签的外观在手机和平​​板电脑的不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将两部手机和平板电脑显示的ActionBar导航选项卡一样的吗?

Is it possible for ActionBar Navigation Tabs to be displayed same on both phones and tablets?

请参阅图像手机和平板电脑如下:

See images for phone and tablets below:

电话:

PHONE:

片剂:

TABLET:

而Android 文档状态......当屏幕是足够宽的标签出现在旁边的操作按钮的操作栏...。 我们不希望他们出现在动作条。任何解决办法?

The Android documentation states "...when the screen is wide enough the tabs appear in the action bar alongside the action buttons...". We don't want them to appear in ActionBar. Any solutions?

推荐答案

动作条标签由<创建href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/widget/ScrollingTabContainerView.java"相对=nofollow> ScrollingTabContainerView 。该<一href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/widget/ActionBarView.java"相对=nofollow> ActionBarView ,实际查看动作条,包含了一个方法<一href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/widget/ActionBarView.java#L364"相对=nofollow> ActionBarView.setEmbeddedTabView 基本上是负责安置选项卡下方或嵌入在动作条<的/ code>。

The ActionBar tabs are created by the ScrollingTabContainerView. The ActionBarView, the actual View for the ActionBar, contains a method ActionBarView.setEmbeddedTabView which is essentially responsible for the placement of the tabs either below or embedded in the ActionBar.

动作条是<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/Activity.java#L1912"相对=nofollow>在活动 ,它使用的<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/view/ActionBarPolicy.java#L55"相对=nofollow> ActionBarPolicy ,以确定何时使用基于一个布尔嵌入式标签价值这被定义为<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/bools.xml#L22"相对=nofollow> 在系统资源,但<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values-port/bools.xml#L18"相对=nofollow> 在纵向模式下。所以,在这个初始化<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/app/ActionBarImpl.java#L217"相对=nofollow> ActionBarImpl.setHasEmbeddedTabs 被称为以确定在何处放置标签。而且,作为文档状态:

When the ActionBar is initialized in Activity, it uses the ActionBarPolicy to determine when to use the embedded tabs based on a boolean value that's defined as true in the system's resources, but false in portrait mode. So, upon this initialization ActionBarImpl.setHasEmbeddedTabs is called to determine where to place the tabs. And also, as the docs state:

...当屏幕足够宽的标签出现在操作栏   旁边的操作按钮

...when the screen is wide enough the tabs appear in the action bar alongside the action buttons

那么,如何从嵌入标签阻止它?

好像你有两个选择,一个是明显的,至少对我来说,比其他更好。

Seems like you have two choices here and one is clearly, at least to me, better than the other.

  1. 您可以使用反射来调用 ActionBarImpl.setHasEmbeddedTabs 键,将其设置为始终。你需要使用反射,因为 ActionBarImpl 是一个内部类,并没有公开动作条方法来指示嵌入标签

  1. You can use reflection to invoke ActionBarImpl.setHasEmbeddedTabs and set it to always be false. You need to use reflection because ActionBarImpl is an internal class and there is no public ActionBar method to indicate when to embed tabs.

您可以停止使用 ActionBar.Tab API并切换到另一种,的像谷歌的 SlidingTabLayout 。这人会要求你使用 ViewPager ,这通常是一个很好的加用反正选项卡时,您甚至可能已经使用它。

You can stop using the ActionBar.Tab API and switch to an alternative, like Google's SlidingTabLayout. This one will require you to use a ViewPager, which is usually is a nice plus when using tabs anyway and you may even already be using it.

下面的例子为:

使用反射

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final ActionBar actionBar = getActionBar();
    // Implement your tabs
    ...
    try {
        final Class<?> clazz = actionBar.getClass();
        final Method embedTabs = clazz.getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
        embedTabs.invoke(actionBar, false);
    } catch (final Exception ignored) {
        // Nothing to do
    }
}

的反思结果

从固定的标签,以滚动的标签页切换

要使用谷歌的 SlidingTabLayout ,你需要在两个类复制到项目。您所需要的类:

To use Google's SlidingTabLayout, you'll need to copy over two classes into your project. The classes you need are:

  • <一个href="http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html"相对=nofollow> SlidingTabLayout
  • <一个href="http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabStrip.html"相对=nofollow> SlidingTabStrip
  • SlidingTabLayout
  • SlidingTabStrip

这是例子布局实施 SlidingTabLayout 会看起来像这样:

An example layout implementing the SlidingTabLayout would looks this like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

    <your_path_to.SlidingTabLayout
          android:id="@+id/slidingTabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
          android:id="@+id/viewPager"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1" />

</LinearLayout> 

充气的布局和初始化后的 SlidingTabLayout 使用 View.findViewById ,你叫 SlidingTabLayout.setViewPager 绑定 ViewPager 的标签。

After inflating the layout and initializing the SlidingTabLayout using View.findViewById, you call SlidingTabLayout.setViewPager to bind the ViewPager to the tabs.

看看谷歌的例子一个完整的项目

滚动标​​签结果

滚动标签被用于一些谷歌的应用程序,如播放存储和播放音乐。对于一个视频短片,说明它们,如果你不熟悉,检查出的Andr​​oid的设计文档在标签的。

Scrollable tabs are used in several of Google's app, like the Play Store and Play Music. For a short video demonstrating them, if you're unfamiliar, check out the Android design docs on tabs.

结论

总之,我会建议使用滚动标签,而不是固定的标签(至少在横向模式下),如果你不希望他们被嵌入。

In short, I would recommend using scrollable tabs rather than fixed tabs (at least in landscape mode), if you don't want them to be embedded.

这篇关于Android的动作条导航标签的外观在手机和平​​板电脑的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆