Computer >> 컴퓨터 >  >> 프로그램 작성 >> Android

Android에서 사용자 정의 알림 레이아웃 및 텍스트 색상을 만드는 방법은 무엇입니까?

<시간/>

이 예는 Android에서 맞춤 알림 레이아웃과 텍스트 색상을 만드는 방법을 보여줍니다.

1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.

2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.

 
3단계 − src/MainActivity.java

에 다음 코드 추가
패키지 com.app.sample;import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.NotificationCompat;import android.os.Bundle;import android.os.Bundle;import android.app.Activity;import android.app.NotificationManager;가져오기 android.app.PendingIntent;가져오기 android.content.Intent;가져오기 android.view.Menu;가져오기 android.view.View;가져오기 android.view.View.OnClickListener;가져오기 android.widget.Button; import android.widget.RemoteViews; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle storedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notificationmain); 버튼 bnotify =(버튼) findViewById(R.id.notification); 버튼 bcustomnotify =(버튼) findViewById(R.id.customnotification); bnotify.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Notification(); // TODO 자동 생성 메소드 스텁 } }); bcustomnotify.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { CustomNotification(); // TODO 자동 생성 메소드 스텁 } }); } public void Notification() { // 알림 제목 설정 문자열 strtitle =getString(R.string.notificationtitle); 문자열 strtext =getString(R.string.notificationtext); 의도 의도 =new Intent(this, NotificationView.class); intent.putExtra("제목", strtitle); intent.putExtra("텍스트", strtext); PendingIntent pIntent =PendingIntent.getActivity(이, 0, 의도, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder 빌더 =new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.logosmall) .setTicker(getString(R.string.notificationticker)) .setContentTitle(getString(R.string.notificationtitle)) .setContentText(getString( R.string.notificationtext)) .addAction(R.drawable.ic_launcher, "액션 버튼", pIntent) .setContentIntent(pIntent) .setAutoCancel(true); NotificationManager 알림 관리자 =(알림 관리자) getSystemService(NOTIFICATION_SERVICE); notificationmanager.notify(0, builder.build()); } 공개 무효 CustomNotification() { RemoteViews remoteViews =새로운 RemoteViews(getPackageName(), R.layout.customnotification); 문자열 strtitle =getString(R.string.customnotificationtitle); 문자열 strtext =getString(R.string.customnotificationtext); 의도 의도 =new Intent(this, NotificationView.class); intent.putExtra("제목", strtitle); intent.putExtra("텍스트", strtext); PendingIntent pIntent =PendingIntent.getActivity(이, 0, 의도, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder 빌더 =새로운 NotificationCompat.Builder(this) .setSmallIcon(R.drawable.logosmall) .setTicker(getString(R.string.customnotificationticker)) .setAutoCancel(true) .setContentIntent(pIntent) .setContent(remoteViews); remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher); remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.androidhappy); remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle)); remoteViews.setTextViewText(R.id.text,getString(R.string.customnotificationtext)); // 알림 관리자 생성 NotificationManager notificationmanager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 알림 관리자로 알림을 빌드합니다. notificationmanager.notify(0, builder.build()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // 메뉴를 부풀립니다. 존재하는 경우 작업 표시줄에 항목을 추가합니다. getMenuInflater().inflate(R.menu.activity_main, 메뉴); true를 반환합니다. }}

4단계 − res/layout/notification.xml에 다음 코드를 추가합니다.

 

5단계 − res/layout/customnotification.xml에 다음 코드를 추가합니다.

    

6단계 − res/layout/notificationview.xml에 다음 코드를 추가합니다.

     

7단계 − Manifests/AndroidManifest.xml에 다음 코드 추가

     <카테고리 android:name=" android.intent.category.LAUNCHER" />   

응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 도구 모음에서 실행 아이콘을 클릭합니다. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오 -

Android에서 사용자 정의 알림 레이아웃 및 텍스트 색상을 만드는 방법은 무엇입니까?