RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 574795
Accepted
Боков Глеб
Боков Глеб
Asked:2020-10-07 15:34:31 +0000 UTC2020-10-07 15:34:31 +0000 UTC 2020-10-07 15:34:31 +0000 UTC

如何在ActionBar菜单中添加主图标的辅助图标?

  • 772

在题目中,辅助图标下,我指的是主图标右下角的小图标。这是一个示例:带有数字的小图标显示通知的数量:

在此处输入图像描述

当然,我想问的是如何添加这个带有数字的小图标,并对其进行编程,以便根据警报的数量显示必要的图标,但是如果您提供的解决方案不是小图标,而是一个用 Java 画成圆圈的数字,那么这个也很合适(也许更好)。


更新

试过katso 的解决方案;活动代码使应用程序崩溃。

E/AndroidRuntime: FATAL EXCEPTION: main

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

我已经遇到过这样的问题并且基于澄清

它在初始化类字段时调用,在调用其构造函数之前创建类的实例时初始化类字段。而这一切发生在 onCreate 方法被调用之前很久。

在方法之前声明变量onCreate并在其中初始化它们。现在这个错误:

在此处输入图像描述

为了以防万一,我发布了完整的应用程序代码。ic_bell任何图像都可以用作图标。部分代码由于某种原因没有显示,但可以从单击“修复”时出现的输入字段中复制。

  • 主活动.java

    导入 android.os.Bundle;导入 android.support.design.widget.FloatingActionButton;导入 android.support.design.widget.Snackbar;导入 android.support.v7.app.AppCompatActivity;导入 android.support.v7.widget.Toolbar;导入 android.view.View;导入 android.view.Menu;导入 android.view.MenuItem;导入 android.widget.TextView;

    公共类 MainActivity 扩展 AppCompatActivity {

    View count;
    TextView notifCount;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        count = menu.findItem(R.id.badge).getActionView();
        notifCount = (TextView)count.findViewById(R.id.notif_count);
    }
    
    private int mNotifCount = 0;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
    
        notifCount.setText(String.valueOf(mNotifCount));
        return super.onCreateOptionsMenu(menu);
    }
    
    private void setNotifCount(int count){
        mNotifCount = count;
        invalidateOptionsMenu();
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
    
        if (id == R.id.badge) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    }

  • menu_main.xml

    <item android:id="@+id/badge"
        android:actionLayout="@layout/actionbar_notifitcation_icon"
        android:showAsAction="always"
        android:icon="@drawable/ic_bell"
        android:title="@string/hotlist" />
    

  • actionbar_notification.xml

    <ImageView
        android:id="@+id/image"
        android:src="@drawable/ic_bell"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"/>
    
    <TextView
        android:id="@+id/notif_count"
        android:layout_width="wrap_content"
        android:minWidth="17dp"
        android:textSize="12sp"
        android:textColor="#ffffffff"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/rounded_square"/>
    

  • rounded_square

java
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    katso
    2020-10-07T16:38:31Z2020-10-07T16:38:31Z

    菜单项

    菜单/menu_actionbar.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/badge"
            android:title="@string/hotlist"
            app:actionLayout="@layout/actionbar_notifitcation_icon"
            app:showAsAction="always" />
    </menu>
    

    按钮布局

    布局/actionbar_notification_icon.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        style="@android:style/Widget.ActionButton"
        android:layout_width="54dp"
        android:layout_height="54dp"
        android:clickable="true">
    
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:src="@drawable/ic_bell" />
    
        <TextView
            android:id="@+id/notif_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_square"
            android:gravity="center"
            android:layout_alignBottom="@+id/image"
            android:layout_alignRight="@+id/image"
            android:textColor="#ffffffff"
            android:padding="4dp"
            android:textSize="12sp" />
    </RelativeLayout>
    

    徽章背景

    可绘制/rounded_square.xml

    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="#ffff0000" />
        <stroke android:color="#ff222222" android:width="2dp"/>
    </shape>
    

    在激活中:

     private int mNotifCount = 1;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_actionbar, menu);
        MenuItem item = menu.findItem(R.id.badge);
        View count = MenuItemCompat.getActionView(item);
        TextView notifCount = (TextView) count.findViewById(R.id.notif_count);
        notifCount.setText(String.valueOf(mNotifCount));
        return super.onCreateOptionsMenu(menu);
    }
    
    private void setNotifCount(int count) {
        mNotifCount = count;
        invalidateOptionsMenu();
    }
    

    例子

    • 7

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5