Android ActionBar Will Not Split On Device

2019-01-28 00:03发布

I know there have been many ActionBar questions but they don't seem to address my problem. I am able to spit the ActionBar in my emulator, but when I run my program on my device (Nexus 7 portrait mode) the ActionBar will not split. All the icons 'pile up' on top, even my tabs create a drop down list. I have tried to force the issue by making the menu items names extremely long and I do have them set to: android:showAsAction="always|withText". Just to be sure, I have taken sample code, ran it on the emulator seen it work and then put it on my device to no avail. Here's my manifest:

<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>

I have scoured the web but cannot find a solution. Any help is appreciated.

2条回答
SAY GOODBYE
2楼-- · 2019-01-28 00:39

I know this question is pretty old but I've found a way to force the actionbar to the buttom on the Nexus 7 (and possible other devices) and I thought I'd share my solution:

Place this code in your Activity:

/**
 * {@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);
    }
}

This will force the internal "split_action_bar_is_narrow" value to true. It might not be the best way to do this but it appears the be the only way I've found.

查看更多
三岁会撩人
3楼-- · 2019-01-28 00:51

According to this SO item, the ActionBar is only splitted when the available width is less than 480dp. According to this article of Google's Dianne Hackborn however, the portrait width of the Nexus 7 is 600dp. So that's the reason there's no splitting.

I agree with you, that the splitting should depend on the relation between available space and items to be shown, not on the available space alone.

查看更多
登录 后发表回答