我正尝试在我的 Android 应用程序的通知区域中设置一个图标。
我注意到自动收报机中的图标似乎被截断了。当自动收报机消失时,通知区域中的图标看起来不错。这是我设置图标的代码:
String msg = "Calculation completed. Tap to view result.";
final Intent restartActivityIntent = new Intent(context, NumberOfPrimesActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, restartActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews mContentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
mContentView.setImageViewResource(R.id.image, R.drawable.two);
mContentView.setTextViewText(R.id.text, msg);
Notification.Builder notificationBuilder = new Notification.Builder(context).setSmallIcon(R.drawable.two).setContentTitle("Sum of Primes").setContentText(msg).setAutoCancel(true).setContentIntent(pendingIntent).setTicker(msg);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notificationBuilder.build());
这是我的自定义通知布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7777"
android:padding="3dp">
<ImageView android:id="@+id/image"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="10dp"
android:contentDescription="alert"
android:src="@drawable/two" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textSize="24sp" />
</LinearLayout>
这是我的 res
目录的样子:
ldpi
文件夹中的two.png
文件为22x22。
最后,附上两张截图。第一个显示带有裁剪图标的股票行情。第二个在自动收报机消失后显示图标。第二个屏幕截图中的图标看起来不错。
更新
mdpi
文件夹中的two.png
文件为48x48。
hdpi
文件夹中的two.png
文件为72x72。
xhdpi
文件夹中的two.png
文件为92x92。
xxhdpi
文件夹中的two.png
文件为144x144。
我正在使用模拟器来测试我的程序。我有两个模拟器。一个是 Nexus 7 (800 x 1280),另一个是 Nexus 4 (768 x 1280)。
在两个模拟器上,自动收报机中的图标都被截断了。
最佳答案
看起来无论出于何种原因,代码 View 中的图标不会自动调整大小,但在通知 View 中会自动调整大小。
根据 Android Iconography , “通知图标必须为 24x24 dp。”,在不同的屏幕密度下转换为以下尺寸:
ldpi: 18 x 18px
mdpi: 24 x 24px
hdpi: 36 x 36px
xhdpi: 48 x 48px
因此,如果您相应地调整 res
文件夹中的 two.png
图标的大小,该图标在行情显示和通知 View 中都不会被截断。
关于android - 如何正确设置通知区图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057370/