如何在Android Studio中显示通知图标。

huangapple go评论74阅读模式
英文:

How to show a notification icon in android studio

问题

我正在构建一个安卓应用程序,允许用户使用Google账号登录。当他们使用Google登录而不通过注册页面注册时,会有一些信息被遗漏。我还有一个屏幕用于更新用户的个人资料信息。

我想在主屏幕上设置一个图标(类似于铃铛图标),显示有一些通知,当用户点击该按钮时,它会提示有一些信息未添加,并将他们重定向到更新个人资料页面。

我想要的图标类似于YouTube上的通知图标(铃铛)。如何在Android Studio中显示通知图标。

是否有任何方法可以实现这一点?

编辑:

activity_main:

<?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。

英文:

I am building an android application where I am letting the users to sign in using google also. When they sign in using google and do not register themselves using the registration page then there are certain information which are left out.I also have a screen which updates the profile information of the user.
I want to set an icon on the home screen(kind of a bell icon) which displays that there are some notification and when the user click on that the button it tells that some information are left to be added and it redirects them to the update profile page.

The icon I want is like the notification icon(bell) in youtube.如何在Android Studio中显示通知图标。

Is there any way I can do this?

EDIT :

activity_main:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@drawable/grdnt&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;TextView
android:id=&quot;@+id/welcome_text&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Welcome&quot;
android:textColor=&quot;#2AF598&quot;
android:textSize=&quot;50dp&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_marginTop=&quot;80dp&quot;
android:gravity=&quot;center&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
app:layout_constraintRight_toRightOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;Button
android:id=&quot;@+id/provide_food&quot;
android:layout_width=&quot;270dp&quot;
android:layout_height=&quot;60dp&quot;
android:layout_below=&quot;@+id/welcome_text&quot;
android:layout_marginTop=&quot;100dp&quot;
android:layout_marginLeft=&quot;80dp&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Do you have Food&quot;
android:textSize=&quot;20dp&quot;
android:background=&quot;@drawable/custombutton&quot;
/&gt;
&lt;Button
android:id=&quot;@+id/need_food&quot;
android:layout_width=&quot;270dp&quot;
android:layout_height=&quot;60dp&quot;
android:layout_below=&quot;@+id/provide_food&quot;
android:layout_marginTop=&quot;50dp&quot;
android:layout_marginLeft=&quot;80dp&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Do you need Food&quot;
android:textSize=&quot;20dp&quot;
android:background=&quot;@drawable/custombutton&quot;
/&gt;
&lt;Button
android:id=&quot;@+id/deliver_food&quot;
android:layout_width=&quot;270dp&quot;
android:layout_height=&quot;60dp&quot;
android:layout_below=&quot;@+id/need_food&quot;
android:layout_marginTop=&quot;50dp&quot;
android:layout_marginLeft=&quot;80dp&quot;
android:gravity=&quot;center&quot;
android:text=&quot;Do you want to Deliver Food&quot;
android:textSize=&quot;20dp&quot;
android:background=&quot;@drawable/custombutton&quot;
/&gt;
&lt;/RelativeLayout&gt;

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);
}
});
}
}

Note: I am not using any custom toolbar, neither am I using any toolbar, my theme for the application is: NoActionBar

答案1

得分: 1

有三种方法可以获得该通知图标,具体取决于您在Activity中使用的主题。

1. 无ActionBar主题: 如果您的活动中未使用任何ActionBar或工具栏,您可以在Activity的布局文件中创建一个具有与通知图标相同高度和宽度(约为28dp)的视图(ImageView或Button),然后将其定位在屏幕的右上角。您可以根据所需的状态更改视图的背景图像。

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

3. ActionBar主题: 如果您正在使用操作栏主题,您可以创建一个具有一个项目的菜单文件,就像选项2中一样,然后重写onCreateOptionsMenu并在其中填充您的菜单。

英文:

There are three ways to get that notification icon depending upon the theme you are using in your Activity.

1. No.ActionBar Theme: If you're not using any ActionBar or toolbar in your activity then you can create a View(ImageView or Button) in your Activity's layout file with Height and Width as same as the notification icon which is around 28dp and then position it at the top right corner of the screen. You can change the background image of the view according to the state you want.

2. Create your own toolbar: You can create your own toolbar and set the menu item in it. While creating the menu item make sure that you select showAsAction=&quot;always&quot; this way your icon is always visible. here is a link for the reference

3.ActionBar Theme: If you're using the action bar theme then you can create a menu file with one item like in Option 2 and then override the onCreateOptionsMenu and inflate then inflate your menu in it.

答案2

得分: 0

Notification notification = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.build();

英文:
  Notification notification  = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.build();

huangapple
  • 本文由 发表于 2020年9月17日 00:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63924516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定