【问题标题】:how to show text below icon in toolbar menu?如何在工具栏菜单中的图标下方显示文本?
【发布时间】:2016-03-24 11:35:11
【问题描述】:

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

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

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

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

【问题讨论】:

  • 我不想使用 ViewPager,而是在寻找 ToolBar 的自定义视图,因为当用户点击更多时,我会显示更多菜单选项。
  • 我也试过了..但做不到..你可以检查codeload.github.com/Yalantis/Side-Menu.Android/zip/masterAndroid自定义垂直下拉图标菜单

标签: android toolbar android-custom-view


【解决方案1】:

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

  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. 引用和点击监听器:

菜单菜单 = your_toolbar.getMenu();

查看 your_menu_view = menu.findItem(R.id.your_id).getActionView(); your_menu_view.setOnClickListener(new View.OnClickListener() { @覆盖 公共无效 onClick(查看 v){ } });

【讨论】:

  • 完美运行,以防您无法单击菜单项: public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_custom, menu); final MenuItem itemMyProfile = menu.findItem(R.id.bt_my_profile);最终菜单 finalMenu = 菜单; itemMyProfile.getActionView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finalMenu.performIdentifierAction(itemMyProfile.getItemId(), 0); } });返回真; }
  • 所以我试图做一个抽屉菜单,但似乎没有工作。我是否在菜单中只有一个项目,然后将所有“项目”放入布局中?
  • material.io/components/android/catalog/navigation-view 满足您长久以来的所有需求。
猜你喜欢
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
相关资源
最近更新 更多