【问题标题】:Android ActionBar Will Not Split On DeviceAndroid ActionBar 不会在设备上拆分
【发布时间】:2012-11-28 13:57:26
【问题描述】:

我知道有很多 ActionBar 问题,但它们似乎没有解决我的问题。我可以在我的模拟器中吐出 ActionBar,但是当我在我的设备上运行我的程序(Nexus 7 纵向模式)时,ActionBar 不会分裂。所有图标都“堆积”在顶部,甚至我的标签也会创建一个下拉列表。我试图通过使菜单项名称非常长来强制解决这个问题,并且我确实将它们设置为:android:showAsAction="always|withText"。可以肯定的是,我已经获取了示例代码,在模拟器上运行它看到它工作,然后把它放在我的设备上无济于事。这是我的清单:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme">
    <activity
        android:name=".MainActivity"
        android:uiOptions="splitActionBarWhenNarrow"
        android:label="@string/title_activity_main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我搜索了网络,但找不到解决方案。任何帮助表示赞赏。

【问题讨论】:

  • 我也有同样的问题。我在某处读到“splitActionBar”仅适用于手机,不适用于平板电脑,但我希望这是一个神话......

标签: android android-actionbar


【解决方案1】:

根据this SO item,只有在可用宽度小于480dp时才拆分ActionBar。然而,根据 Google 的 Dianne Hackborn 的this article,Nexus 7 的纵向宽度为 600dp。所以这就是没有分裂的原因。

我同意你的观点,拆分应该取决于可用空间和要显示的项目之间的关系,而不是仅仅取决于可用空间。

【讨论】:

  • Ridcully,感谢您挖掘它。我只是很惊讶会出现这种情况。
【解决方案2】:

我知道这个问题已经很老了,但我找到了一种方法可以将操作栏强制放在 Nexus 7(以及可能的其他设备)的按钮上,我想我会分享我的解决方案:

将此代码放入您的活动中:

/**
 * {@inheritDoc}
 */
@Override
public Resources getResources() {
    return new ResourceFix(super.getResources());
}

private class ResourceFix extends Resources {
    private int targetId = 0;

    ResourceFix(Resources resources) {
        super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
        targetId = Resources.getSystem().getIdentifier("split_action_bar_is_narrow", "bool", "android");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean getBoolean(int id) throws Resources.NotFoundException {
        return targetId == id || super.getBoolean(id);
    }
}

这将强制内部“split_action_bar_is_narrow”值为真。 这可能不是最好的方法,但它似乎是我找到的唯一方法。

【讨论】:

  • 不错的技巧。希望它得到一些可见性。这是一个常见的问题
  • 难以置信。干得好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多