Tim*_*len 2 java xml android android-xml android-menu
我有以下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>
Run Code Online (Sandbox Code Playgroud)
这给了我一行三个菜单按钮.但是,我想将它们分成两行,第一行有2个按钮,整个底行有帮助按钮.我已经尝试过对前两个进行分组,但这并没有成功.如何在XML中强制换行?
据我所知,没有办法在菜单中强制换行.如果你考虑一些场景,这实际上是有意义的.
例如,假设您在横向方向上有平板电脑(例如,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" />
Run Code Online (Sandbox Code Playgroud)
如果你遵循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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,例如,如果您增加第一个项目的宽度,您将在菜单中获得两行.但这可能是不可取的,因为您无法预测将在不同设备上使用的文本样式,大小和字体.

| 归档时间: |
|
| 查看次数: |
4991 次 |
| 最近记录: |