【问题标题】:How to get the rounded edges for notifications如何获得通知的圆角边缘
【发布时间】:2016-01-10 09:14:29
【问题描述】:

我注意到通知布局有圆角

我的有尖角

我尝试用可绘制的圆角来圆角

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#4d4d4d"/>
    <corners android:radius="1dip"/>
</shape>

我接近了,但它真的不一样。

如何以标准方式获得圆角边缘?

编辑

这是我的代码。在我的主要活动中设置了面板。归功于here

NotificationPanel nPanel = new NotificationPanel(this);

这是我的NotificationPanel 课程

public class NotificationPanel {

    private Context parent;
    private NotificationManager nManager;
    private NotificationCompat.Builder nBuilder;
    private RemoteViews remoteView;

    public NotificationPanel(Context parent) {
        // TODO Auto-generated constructor stub
        this.parent = parent;
        nBuilder = new NotificationCompat.Builder(parent)
                .setContentTitle("Parking Meter")
                .setSmallIcon(R.drawable.button_play)
                .setOngoing(true);

        remoteView = new RemoteViews(parent.getPackageName(), R.layout.notificationview);

        //set the button listeners
        setListeners(remoteView);
        nBuilder.setContent(remoteView);

        nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(2, nBuilder.build());
    }

    public void setListeners(RemoteViews view){
        //listener 1
        Intent volume = new Intent(parent,NotificationReturnSlot.class);
        volume.putExtra("DO", "volume");
        PendingIntent btn1 = PendingIntent.getActivity(parent, 0, volume, 0);
        view.setOnClickPendingIntent(R.id.btn1, btn1);

        //listener 2
        Intent stop = new Intent(parent, NotificationReturnSlot.class);
        stop.putExtra("DO", "stop");
        PendingIntent btn2 = PendingIntent.getActivity(parent, 1, stop, 0);
        view.setOnClickPendingIntent(R.id.btn2, btn2);
    }

    public void notificationCancel() {
        nManager.cancel(2);
    }
}

这是通知视图布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="volume" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Stop" />

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/msglbl" />

</LinearLayout>

【问题讨论】:

  • 您的栏是否也显示在通知列表中?您是否想要与屏幕截图中完全相同的角,还是更喜欢继承默认的 android 样式(因为这可能因版本/手机而异)?
  • 我希望角落与其他通知相同。我还希望所有设备的一致性。通知列表是什么意思?如果您的意思是它们都从屏幕顶部下拉,那么是的。
  • 是的,我就是这个意思。您是否有一些代码可以显示在其中创建框? // 没关系,现在阅读您的编辑。
  • 是的,谢谢。我刚刚添加了它。
  • 我可能错了,但请尝试从LinearLayout 中删除android:background 属性。也许这覆盖了背景中的默认背景(带角)。

标签: android


【解决方案1】:

您可能想尝试向通知添加操作,然后根据自己的喜好对其进行调整,而不是使用侦听器和其他自定义设置自己的布局 - 它可能会在各种 API 中更好地工作并且看起来更标准:

// Set the style
NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(strMessage);

// Build the notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setContentTitle(title)
            .setContentText(message)
            .setTicker(ticker)
            .setColor(context.getResources().getColor(R.color.notification_bg))
            .setSmallIcon(R.drawable.status_bar_ic)
            .setContentIntent(intent)
            .setStyle(bigStyle)
            .setWhen(0)
            .setAutoCancel(true)
            .setStyle(bigStyle)
            .addAction(R.drawable.notification_volume_ic, getString(R.string.volume), pendingVolume)
            .addAction(R.drawable.notification_stop_ic, getString(R.string.stop), pendingStop)
            .setOngoing(true);

notificationBuilder.build();

【讨论】:

  • 谢谢。这个帖子here也帮了大忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 2013-06-24
  • 1970-01-01
相关资源
最近更新 更多