如何在工具栏菜单中显示图标下面的文字?

5
基本上我们使用带图标的工具栏菜单,有时我们也会同时使用图标/文字。 图标和文本一起水平显示,就像下面这张图片一样。

enter image description here

在正常情况下,它会发生,但我想像下面的图像那样垂直显示菜单图标/文本。

enter image description here

我已经使用这个xml完成了布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="horizontal"
    android:padding="@dimen/default_margin">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/bg_timeline"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/label_timeline" />

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/default_margin"
        android:drawableTop="@drawable/ic_about_large"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/label_about" />

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_achivement_active"
        android:layout_marginLeft="@dimen/default_margin"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/label_achievements" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/ic_more"
        android:background="?selectableItemBackground"
        android:gravity="center"
        android:layout_marginLeft="@dimen/default_margin"
        android:layout_weight="1"
        android:text="@string/label_more" />
</LinearLayout>

我的问题是如何实现工具栏/操作栏,当菜单项放置在每个图标下面的文本作为一个整体时。有没有什么方法可以做到这一点?

我不想使用ViewPager,而是寻找自定义视图用于工具栏,当用户点击"更多"时,我将显示更多的菜单选项。 - androidcodehunter
1
我也尝试了,但没能实现。你可以查看 https://codeload.github.com/Yalantis/Side-Menu.Android/zip/master ,这是Android自定义垂直下拉图标菜单。 - Amitabha Biswas
1个回答

6
您可以使用工具栏方法 inflateMenu 来填充自定义菜单布局。以下是一个小片段:
  1. 在菜单文件夹中定义您的自定义菜单your_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/your_id"
        android:title="@string/your_string"
        android:icon="@drawable/your_menu_icon"
        app:actionLayout="@layout/your_custom_menu_layout"
        app:showAsAction = "always"
        />
</menu>
现在定义your_custom_menu_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingRight="10dp">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:src="@drawable/your_menu_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingLeft="0dp"
        android:text="your_text"
        android:textColor="@color/white"
        android:textSize="9dp" />
</LinearLayout>
  1. 第三步,使用工具栏在你的Java代码中填充菜单项:

your_toolbar.inflateMenu(R.menu.your_menu);

  1. 参考和点击监听器:

Menu menu = your_toolbar.getMenu();

View your_menu_view = menu.findItem(R.id.your_id).getActionView(); your_menu_view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });


1
如果无法执行菜单项点击,则完美运行:public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_custom, menu); final MenuItem itemMyProfile = menu.findItem(R.id.bt_my_profile); final Menu finalMenu = menu; itemMyProfile.getActionView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finalMenu.performIdentifierAction(itemMyProfile.getItemId(), 0); } }); return true; } - Rob
所以我正在尝试制作一个抽屉式菜单,但似乎无法正常工作。我只需要在菜单中放置一个项目,然后将所有的“项”放置在布局中吗? - justdan0227
https://material.io/components/android/catalog/navigation-view/ 这是你长期所需的一切。 - Kaveesh Kanwal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接