android - 如何在工具栏中显示带有文本的图标

标签 android android-toolbar optionmenu

我在工具栏中添加带有文本的图标时遇到问题。我尝试了以下代码:

  public boolean onCreateOptionsMenu(Menu menu) {
    menu.clear();
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_new_group, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.createGroup:
            createNewGroup();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

menu_new_group.xml

 <?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/createGroup"
        android:icon="@drawable/single_tick"
        android:orderInCategory="100"
        android:title="Create"
        app:showAsAction="always|withText" />
</menu>

工具栏中仅显示单个勾号。不显示标题。请帮忙。

更新后的代码:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/createGroup"
        android:icon="@drawable/single_tick"
        android:orderInCategory="100"
        android:showAsAction="withText|always"
        android:title="Create" />
</menu>

仍然不适合我。请查看以下屏幕截图:

enter image description here

这里没有显示图标。我想要工具栏上的图标和文本。

activity_new_group.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBarNewGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/toolbar_color"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/margin10">

        <com.almabay.almachat.circularImageView.CircularImageView
            android:id="@+id/groupPic"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_gravity="bottom|center_horizontal" />

        <com.almabay.almachat.circularImageView.CircularImageView
            android:id="@+id/iv_camera_group"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="top|right"
            android:layout_marginTop="@dimen/margin30"
            android:background="@drawable/color"
            android:src="@drawable/camera1" />
    </FrameLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/margin10"
        android:layout_marginTop="@dimen/margin20"
        android:background="@drawable/edittext_bottom_border_orange"
        android:hint="Type group subject here ..."
        android:paddingBottom="@dimen/padding10" />

    <ListView
        android:id="@+id/lv_friends"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

最佳答案

我已经通过以下代码解决了这个问题:

1- 在菜单文件夹中创建菜单

<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=".MainActivity">

<item
    android:id="@+id/action_sign"
    android:title="签到"
    app:showAsAction="always|withText"
    app:actionLayout="@layout/menu_sign" />

2- app:actionLayout 是关键字,然后创建名为 menu_sign 的布局并将 textView 放入其中。图标将由 drawableLeft 显示

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:clickable="true"
   android:drawableLeft="@mipmap/ic_user_sign"
   android:gravity="center"
   android:orientation="vertical"
   android:text="@string/text"
   android:drawablePadding="@dimen/margin_5"
   android:paddingLeft="@dimen/margin_10"
   android:paddingRight="@dimen/margin_10"
   android:textColor="@android:color/white" />

3- 为您的菜单添加监听器

super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_sign, menu);
    signItem = menu.findItem(R.id.action_sign);
    signItem.getActionView().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onOptionsItemSelected(signItem);
        }
    });

4- 然后你会得到菜单和监听器

关于android - 如何在工具栏中显示带有文本的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35669181/

相关文章:

android - 数据库磁盘镜像格式错误或文件已加密或不是数据库

android - 如何在不损失 Android 分辨率的情况下减小位图的大小?

android - 上下文操作栏不覆盖我的工具栏

python - tkinter 选项菜单不显示选择

android - 听菜单打开

android - 选项菜单关闭时调用的方法

android - 如何抑制 "Can be replaced with foreach call"警告

android - 如果有一种方法可以在不上传的情况下在 google play 上检查设备兼容性

Android - 我怎样才能实现这种类型的工具栏?

android - 如何在抽屉导航顶部创建工具栏(Android/kotlin)