Android的remoteviews的通知 [英] Android remoteviews in notification

查看:221
本文介绍了Android的remoteviews的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个音乐播放器应用程序。这名球员是在一个服务。当一首歌曲开始播放我要显示与remoteViews一个自定义通知。

I am developing a music player application. The player is in a service. When a song starts to play i want to show a custom notification with remoteViews.

是我设计的布局是:

但遗憾的是该通知是建立在textviews都消失了:

but unfortunately the textviews are gone when the notification is built:

在code的布局如下:

the code for the layout is as follows:

notification_player.xml

notification_player.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:id="@+id/album_art" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/wrapper"
        android:layout_toRightOf="@+id/album_art"
        android:layout_alignBottom="@+id/album_art">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/player"
            android:layout_marginBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/prev"
                android:layout_marginRight="5dp"
                android:src="@drawable/ic_fast_rewind"
                android:layout_gravity="center_vertical" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:background="#82c7c7c7"></LinearLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/play"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center_vertical"
                android:background="@drawable/ic_play_arrow" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:background="#82c7c7c7" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:layout_marginLeft="5dp"
                android:layout_gravity="center_vertical"
                android:background="@drawable/ic_fast_forward" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:id="@+id/divider_horizontal"
            android:layout_above="@+id/player"
            android:background="#82c7c7c7"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"></LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_above="@+id/divider_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/title"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="25dp"
                android:text="Title"
                style="@style/NotificationTitle"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="singer"
                android:id="@+id/singer"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                style="@style/NotificationText"/>
        </LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/close"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/ic_close" />

    </RelativeLayout>
    </RelativeLayout>

code的服务,正显示出通知:

Code in service that is showing the notification:

NotificationCompat.Builder builder;
RemoteViews rv = new RemoteViews(getPackageName(),R.layout.notification_player);
        rv.setImageViewBitmap(R.id.album_art,aq.getCachedImage(item.getImage()));
rv.setTextViewText(R.id.title, item.getTitle());
rv.setTextViewText(R.id.singer, item.getSinger());
rv.setOnClickPendingIntent(R.id.close, delete);


builder = new NotificationCompat.Builder(getApplicationContext())
.setContent(rv)
.setSmallIcon(ir.farzamhabibi.moozik.R.drawable.ic_launcher);

startForeground(10, builder.build());

任何想法是什么问题?

Any idea what the problem is?

编辑:

我尝试设置的LinearLayout高度来包装内容和去除边缘

I tried setting the LinearLayout height to wrap content and removing the margins

我甚至尝试,即使在所有的家长在所有布局添加一个TextView但似乎该通知删除所有textviews。

I even tried adding a textView in all the layouts even in the parent of all but it seems the notification is removing all the textviews.

推荐答案

我发现了什么是错误的。
在我来说,我是想显示的布局,比默认的通知布局更大,作为结果它被COM pressing我的布局以适应默认通知的大小。

I found out what was wrong. In my case i was trying to show a layout which was greater than the default notification layout and as the result it was compressing my layout to fit the size of default notification.

我改变了看法是这样的:

I changed the views like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/album_art" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/wrapper"
        android:descendantFocusability="blocksDescendants"
        android:layout_toRightOf="@+id/album_art">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="false"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:id="@+id/info"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="title"
                android:textStyle="bold"
                />

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="singer"
                />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:descendantFocusability="blocksDescendants"
            android:layout_alignParentBottom="false"
            android:layout_centerHorizontal="true"
            android:id="@+id/player"
            android:layout_below="@+id/divider_horizontal"
            android:layout_marginTop="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/prev"
                android:layout_marginRight="20dp"
                android:src="@drawable/ic_fast_rewind"
                android:layout_gravity="center_vertical" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:background="#82c7c7c7"></LinearLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/play"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="20dp"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_pause" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:background="#82c7c7c7" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/next"
                android:layout_marginLeft="20dp"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_fast_forward" />

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:id="@+id/divider_horizontal"
            android:background="#82c7c7c7"
            android:layout_below="@+id/info"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"></LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/close"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/ic_close" />

    </RelativeLayout>

</RelativeLayout>

和通知我的方法加入这解决了这个问题下面几行:

And in the notification method i added the following lines which solved the problem:

RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notification_player);


        builder = new NotificationCompat.Builder(getApplicationContext())
                .setContent(rv)
                .setTicker("IranGrammy playing")
                .setAutoCancel(true)
                .setSmallIcon(ir.farzamhabibi.moozik.R.drawable.ic_launcher);

        rv.setImageViewBitmap(R.id.album_art, aq.getCachedImage(item.getImage()));
        rv.setTextViewText(R.id.title, item.getTitle());
        rv.setTextViewText(R.id.text, item.getSinger());
        rv.setOnClickPendingIntent(R.id.close, delete);
        rv.setOnClickPendingIntent(R.id.play, playPI);
        rv.setOnClickPendingIntent(R.id.next, nextPI);
        rv.setOnClickPendingIntent(R.id.prev, prevPI);
        Notification notif = builder.build();
        notif.bigContentView = rv;
        startForeground(10, notif);

这篇关于Android的remoteviews的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆