Android 菜单换行符

发布于 2024-11-08 21:02:51 字数 718 浏览 3 评论 0 原文

我有以下 Android 菜单 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/programma"
          android:icon="@android:drawable/ic_menu_agenda"
          android:title="@string/programma" />
    <item android:id="@+id/settings"
          android:icon="@android:drawable/ic_menu_preferences"
          android:title="@string/settings" />
    <item android:id="@+id/help"
          android:icon="@android:drawable/ic_menu_help"
          android:title="@string/help" />
</menu>

这在一行上提供了三个菜单按钮。但是,我想将它们分成两行,第一行有 2 个按钮,整个底行有帮助按钮。我已经尝试将前两个分组,但这并没有成功。 如何在 XML 中强制换行?

I have the following Android menu XML file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/programma"
          android:icon="@android:drawable/ic_menu_agenda"
          android:title="@string/programma" />
    <item android:id="@+id/settings"
          android:icon="@android:drawable/ic_menu_preferences"
          android:title="@string/settings" />
    <item android:id="@+id/help"
          android:icon="@android:drawable/ic_menu_help"
          android:title="@string/help" />
</menu>

That gives me three menu buttons on one line. However, I want to divide them in two lines, with 2 buttons on the first row, and the help button on the entire bottom row. I already tried grouping the first two, but that didn't do the trick.
How can I force a line break in the XML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2024-11-15 21:02:51

据我所知,没有办法在菜单中强制换行。如果您考虑某些场景,这实际上是有道理的。

例如,假设您有横向平板电脑(例如 Galaxy Tab)。它具有相当大的水平空间和相对较小的高度。因此,如果在这种情况下强制换行,就会浪费空间。

在此处输入图像描述


我对此做了更多调查。有一个名为 MenuBuilder 用于管理选项菜单。它使用 icon_menu_layout 绘制菜单的布局资源。 此处 是这个资源:

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

如果您按照 IconMenuView 实现,你会发现一个有趣的函数,名为 layoutItems 用于计算菜单项的布局。您只能通过人为增加菜单项的宽度来影响此函数的逻辑。没有可用的换行符。

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

因此,例如,如果您增加第一个项目的宽度,您将在菜单中看到两行。但这可能是不可取的,因为您无法预测将在不同设备上使用的文本样式、大小和字体。

在此处输入图像描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文