fragment 中怎么设置toolbar的menu

fragment 中怎么设置toolbar的menu,第1张

放两个panel 在窗体上,都设置属性Dock 为 top , menu 放在上面panel里,toolbar放在下面的panel

Android Menu的文字有两种设置方式:

在 menu.xml中设置。

在java代码onCreateOptionsMenu中设置。

以下为示例代码:

在menu.xml中指定title即可

<item android:id="@+id/menu"

android:title="文字"/>

重载onCreateOptionsMenu(Menu menu)方法,并调用API。

menu.add((int groupId, int itemId, int order, charsequence title) .setIcon(drawable ID)

add()方法的四个参数,依次是:

1、组别,如果不分组的话就写Menu.NONE。

2、Id,这个很重要,Android根据这个Id来确定不同的菜单 。

3、顺序,哪个菜单项在前面由这个参数的大小决定 。

4、文本,菜单项的显示文本。

在Fragment中添加一个布局容器,并设置ID,在Activity中findbyview找到后,就可以动态添加了。

代码如下:

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<Button

android:id="@+id/first_button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/first_button"/>

<Button

android:id="@+id/second_button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/second_button"/>

<Button

android:id="@+id/third_button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/third_button"/>

</LinearLayout>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11433459.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存