6

我知道有很多 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>

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

4

2 回答 2

7

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

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

于 2012-11-30T09:04:26.167 回答
7

我知道这个问题已经很老了,但我找到了一种方法来强制操作栏在 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”值为真。这可能不是最好的方法,但它似乎是我找到的唯一方法。

于 2014-05-20T08:52:14.647 回答