gpt4 book ai didi

java - 如何在工具栏上的每个菜单项之间添加分隔符

转载 作者:行者123 更新时间:2023-11-30 01:40:55 25 4
gpt4 key购买 nike

enter image description here(here is a pic of my toolbar with the menu items)

我有一个包含 4 个菜单项的工具栏,但我想不出在每个项目之间添加分界线的方法。这是我的工具栏 .xml 文件

已编辑

这是我到目前为止所拥有的,但它仍然没有创建分隔符

    <menu xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.mycompany.materialtest.MainActivity">

<group android:id="@+id/homegroup"
android:checkableBehavior="single">
<item
android:id="@+id/home"
android:title="home"
android:orderInCategory="1"
android:icon="@drawable/homeclicked"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/searchgroup"
android:checkableBehavior="single">
<item
android:id="@+id/search"
android:title="search"
android:orderInCategory="2"
android:icon="@drawable/search"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/chatgroup"
android:checkableBehavior="single">
<item
android:id="@+id/chat"
android:title="chat"
android:orderInCategory="3"
android:icon="@drawable/chat"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/profilegroup"
android:checkableBehavior="single">
<item
android:id="@+id/profile"
android:title="profile"
android:orderInCategory="4"
android:icon="@drawable/user"
app:showAsAction="always">
</item>
</group>
</menu>

我还在 mainactivity.java 上分离了菜单项。如果有帮助的话,这里是代码。

public void setupEvenlyDistributedToolbar(Toolbar toolbar) {
// Use Display metrics to get Screen Dimensions
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);


toolbar.inflateMenu(R.menu.menu_bottombar);


// Add 10 spacing on either side of the toolbar
toolbar.setContentInsetsAbsolute(10, 10);

// Get the ChildCount of your Toolbar, this should only be 1
int childCount = toolbar.getChildCount();
// Get the Screen Width in pixels
int screenWidth = metrics.widthPixels;

// Create the Toolbar Params based on the screenWidth
Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, Toolbar.LayoutParams.WRAP_CONTENT);

// Loop through the child Items
for (int i = 0; i < childCount; i++) {
// Get the item at the current index
View childView = toolbar.getChildAt(i);
// If its a ViewGroup
if (childView instanceof ViewGroup) {
// Set its layout params
childView.setLayoutParams(toolbarParams);
// Get the child count of this view group, and compute the item widths based on this count & screen size
int innerChildCount = ((ViewGroup) childView).getChildCount();
int itemWidth = (screenWidth / innerChildCount);
// Create layout params for the ActionMenuView
ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth, Toolbar.LayoutParams.WRAP_CONTENT);
// Loop through the children
for (int j = 0; j < innerChildCount; j++) {
View grandChild = ((ViewGroup) childView).getChildAt(j);
if (grandChild instanceof ActionMenuItemView) {
// set the layout parameters on each View
grandChild.setLayoutParams(params);
}
}
}
}
}

}`

任何帮助都将非常感谢!

最佳答案

用 XML 试试这个

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/home"
android:checkableBehavior="single">
<item
android:id="@+id/homegroup"
android:title="@string/next"
android:orderInCategory="1"
android:icon="@drawable/homeclicked"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/searchgroup"
android:checkableBehavior="single">
<item
android:id="@+id/search"
android:title="@string/next"
android:orderInCategory="2"
android:icon="@drawable/search"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/chatgroup"
android:checkableBehavior="single">
<item
android:id="@+id/chat"
android:title="@string/next"
android:orderInCategory="3"
android:icon="@drawable/chat"
app:showAsAction="always">
</item>
</group>
<group android:id="@+id/profilegroup"
android:checkableBehavior="single">
<item
android:id="@+id/profile"
android:title="@string/next"
android:orderInCategory="4"
android:icon="@drawable/profile"
app:showAsAction="always">
</item>
</group>
</menu>

关于java - 如何在工具栏上的每个菜单项之间添加分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34515806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com