首页 > 解决方案 > 如何在android studio中显示通知图标

问题描述

我正在构建一个 android 应用程序,让用户也可以使用 google 登录。当他们使用谷歌登录并且不使用注册页面进行注册时,某些信息会被遗漏。我还有一个屏幕可以更新用户的个人资料信息。我想在主屏幕上设置一个图标(一种铃铛图标),显示有一些通知,当用户单击该按钮时,它会告诉一些信息有待添加,并将它们重定向到更新个人资料页。

我想要的图标就像 youtube 中的通知图标(铃铛)。youtube 图标

有什么办法可以做到这一点吗?

编辑 :

活动主:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/grdnt"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/welcome_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textColor="#2AF598"
        android:textSize="50dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="80dp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/provide_food"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:layout_below="@+id/welcome_text"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="80dp"
        android:gravity="center"
        android:text="Do you have Food"
        android:textSize="20dp"
        android:background="@drawable/custombutton"
        />

    <Button
        android:id="@+id/need_food"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:layout_below="@+id/provide_food"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="80dp"
        android:gravity="center"
        android:text="Do you need Food"
        android:textSize="20dp"
        android:background="@drawable/custombutton"
        />

    <Button
        android:id="@+id/deliver_food"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:layout_below="@+id/need_food"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="80dp"
        android:gravity="center"
        android:text="Do you want to Deliver Food"
        android:textSize="20dp"
        android:background="@drawable/custombutton"
        />

</RelativeLayout>

MainActivity.java:

package com.example.helping_hands_individual;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button provide, need,deliver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        provide = (Button)findViewById(R.id.provide_food);
        need = (Button)findViewById(R.id.need_food);
        deliver = (Button)findViewById(R.id.deliver_food);

        provide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Provide_food.class);
                startActivity(intent);
            }
        });

    }
}

注意:我没有使用任何自定义工具栏,也没有使用任何工具栏,我的应用程序主题是:NoActionBar

标签: javaandroidandroid-studio

解决方案


根据您在 Activity 中使用的主题,可以通过三种方式获取通知图标。

1. No.ActionBar 主题:如果您没有在活动中使用任何ActionBar 或工具栏,那么您可以在活动的布局文件中创建一个视图(ImageView 或按钮),其高度和宽度与通知图标相同,大约为28dp然后将其放置在屏幕的右上角。您可以根据您想要的状态更改视图的背景图像。

2.创建自己的工具栏:您可以创建自己的工具栏并在其中设置菜单项。在创建菜单项时,请确保您选择showAsAction="always"这种方式,您的图标始终可见。这是一个参考链接

3.ActionBar 主题:如果您使用的是操作栏主题,那么您可以创建一个菜单文件,其中包含选项 2 中的一个项目,然后覆盖 onCreateOptionsMenu 并膨胀,然后在其中膨胀您的菜单。


推荐阅读