Android menu line break

前端 未结 1 1119
孤独总比滥情好
孤独总比滥情好 2020-12-20 02:50

I have the following Android menu XML file:




        
相关标签:
1条回答
  • 2020-12-20 03:06

    As far as I know there is no way to force line break in menu. This actually makes sense if you consider some scenarios.

    For example, imagine you have tablet (for example, Galaxy Tab) in landscape orientation. It has quite a lot of horizontal space and relatively small height. So, it would be a waste of space if you force line break in such case.

    enter image description here


    I've done more investigation on this. There is a class called MenuBuilder which is used to manage options menu. It uses icon_menu_layout layout resource to draw menus. Here is this resource:

    <com.android.internal.view.menu.IconMenuView
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+android:id/icon_menu"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:rowHeight="66dip"
       android:maxItems="6"
       android:maxRows="2"
       android:maxItemsPerRow="6" />
    

    And if you follow to IconMenuView implementation, you'll find one interesting function called layoutItems which is used to calculate layout for menu items. You can only influence this function's logic by artificially increasing width of menu items. No line breaks are available.

    private void layoutItems(int width) {
        int numItems = getChildCount();
        if (numItems == 0) {
            mLayoutNumRows = 0;
            return;
        }
    
        // Start with the least possible number of rows
        int curNumRows =
                Math.min((int) Math.ceil(numItems / (float) mMaxItemsPerRow), mMaxRows);
    
        /*
         * Increase the number of rows until we find a configuration that fits
         * all of the items' titles. Worst case, we use mMaxRows.
         */
        for (; curNumRows <= mMaxRows; curNumRows++) {
            layoutItemsUsingGravity(curNumRows, numItems);
    
            if (curNumRows >= numItems) {
                // Can't have more rows than items
                break;
            }
    
            if (doItemsFit()) {
                // All the items fit, so this is a good configuration
                break;
            }
        }
    }
    

    So, for example, if you increase width of first item, you'll get two lines in menu. But this is probably undesirable, because you can't predict text style, size and font that will be used on different devices.

    enter image description here

    0 讨论(0)
提交回复
热议问题