自定义通知查看 [英] Custom Notification View

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

问题描述

我想创建一个通知图标视图,类似于Google+应用的通知。所不同的将是我需要能够改变颜色在运行时,其中作为Google+的图标灰色或红色,所以我假设他们使用的是StateListDrawable。

I would like to create a notification icon view that looks similar to the Google+ app's notification. The difference will be that I need to be able to change the color at runtime where as the Google+ icons gray or red so I'm assuming they are using a StateListDrawable.

,这是什么最好的方法?我想preFER有圆形的剪切角,必须有一个可绘里的选项。这个自定义视图将被放置在操作栏为好。我仍然需要以到Android回应:后台状态列表可绘制这样我就可以有点击和选择按照工作

What is the best approach for this? I'd prefer to have the rounded clipped corners and have the option to have a drawable inside. This custom view will be placed in the Action Bar as well. I still need the view to respond to android:background state list drawables so I can have the click and selected accordance working.

这个自定义视图将被放置在操作栏中为好。

This custom view will be placed in the action bar as well.

推荐答案

我通过执行以下操作解决了这个。

I solved this by doing the following.

创建这使圆角外形采用了纯色。这也增加了一个半透明的黑色,给它一个pressed看起来对一个blackground。 RES /绘制/ shape_notification.xml

Created this to make the rounded corner shape with a solid color. This also adds a translucent black to give it a pressed look against a blackground. res/drawable/shape_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <stroke android:color="#33000000" android:width="2dp"/>
    <corners android:radius="4dp" />
    <solid android:color="#99333333"/>
</shape>

图层绘制将用作在动作栏项目的实际绘制。它有重叠的扳手图标背景(上面写的)。 RES /绘制/ layer_customizer.xml

The layer drawable will be used as the actual drawable on the action bar item. It has the background (written above) overlayed with the wrench icon. res/drawable/layer_customizer.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/shape_notification" />
    <item android:drawable="@drawable/ic_menu_preferences" />
</layer-list>

Java的code来改变颜色。目标视图是分配的layer_customizer可绘制的对象。传递的颜色会改变shape_notification.xml的固体标记颜色。

Java code to change the color. The target view is the object that is assigned the layer_customizer drawable. The color passed in will change the shape_notification.xml's solid tag color.

public static void setCustomizerDrawableColor(final View target, final int color) {
  final Drawable d = target.getDrawable();
  LayerDrawable layer = (LayerDrawable)d;
  GradientDrawable gradient = (GradientDrawable)layer.getDrawable(0);
  gradient.setColor(color);
  gradient.invalidateSelf();
  layer.invalidateSelf();
  target.invalidate();
}

创建使用这些层的布局。 RES /布局/ actionview_customizer.xml

Create a layout using these layers. res/layout/actionview_customizer.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ActionViewCustomizer"
    android:src="@drawable/layer_customizer"
    android:contentDescription="@string/customize"
    style="@style/ActionBarButton" />

要获得自定义布局投入了动作条中添加该菜单项到其中: RES /菜单/ actionbar_main.xml

To get the custom layout to put into the ActionBar add this menu item into it: res/menu/actionbar_main.xml

<item android:id="@+id/MenuItemCustomize"
  android:icon="@drawable/layer_customizer"
  android:title="@string/customize"
  android:showAsAction="always"
  android:actionLayout="@layout/actionview_customizer"
   />

然后加载操作栏使用code得到的句柄按钮后。这发生在你的活动。

Then after loading the Action Bar use this code to get the handle to the button. This happens in your Activity.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actionbar_main, menu);
    final ActionBar actionBar = getActionBar();
    final MenuItem customizerItem = menu.findItem(R.id.MenuItemCustomize);
    View v = customizerItem.getActionView();
    customizerActionView = (ImageButton) v;
    customizerActionView.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            onOptionsItemSelected(customizerItem);
        }
    });
}

如果你想看到完整的源代码一起工作看程序的源$ C ​​$ C我用这 <一个href="http://$c$c.google.com/p/motivatormaker-android/source/browse/MakeMotivator/src/com/futonredemption/makemotivator/activities/MainActivity.java">http://$c$c.google.com/p/motivatormaker-android/source/browse/MakeMotivator/src/com/futonredemption/makemotivator/activities/MainActivity.java

If you want to see the full source working together look at the app source code I use this in. http://code.google.com/p/motivatormaker-android/source/browse/MakeMotivator/src/com/futonredemption/makemotivator/activities/MainActivity.java

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

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