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

标签 android toolbar android-custom-view

基本上我们使用带图标的工具栏菜单,有时我们也使用图标/文本。图标和文本水平显示,如下图所示

enter image description here

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

enter image description here

我已经用这个xml做了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>

我的问题是当菜单项像文本一样放置在每个图标下方时,如何实现工具栏/操作栏。有办法做到这一点吗?

最佳答案

您可以使用工具栏方法 inflateMenu 来扩充自定义菜单布局。下面是一个小 fragment :

  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>
  1. 现在定义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) { } });

关于android - 如何在工具栏菜单中显示图标下方的文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199215/

相关文章:

android - 从 react-native-static-server : undefined is not an object (evaluating 'this.staticServer.start' ) 启动 StaticServer 时出错

android - 首先在 child 的 parent 上调用 removeView()

java - 工具栏干扰使用 SearchView 过滤 RecyclerView

ios - InputAccessoryView 的 subview 工具栏按钮不起作用

android - 不同设备的自定义 View 大小

android - 每 10 秒获取一次 PARTIAL_WAKELOCK

Eclipse Juno 透视工具栏

java - 将 JMenu ButtonGroups 与 JToolbar ButtonGroups 同步

android - android 中自定义 ListView 的性能问题?

android - 扩展Widget的样式继承