自定义通知视图
我想创建一个类似于 Google+ 应用程序通知的通知图标视图。不同之处在于我需要能够在运行时更改颜色,其中 Google+ 图标为灰色或红色,因此我假设它们正在使用 StateListDrawable。
对此最好的方法是什么?我更喜欢有圆角的剪角,并且可以选择在里面有一个可绘制的。此自定义视图也将放置在操作栏中。我仍然需要视图来响应 android:background state 列表可绘制对象,以便我可以让单击和选定的一致工作。
此自定义视图也将放置在操作栏中。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过执行以下操作解决了这个问题。
创建这个是为了使圆角形状具有纯色。这还添加了半透明的黑色,使其在黑底上看起来有压迫感。
res/drawable/shape_notification.xml
可绘制图层将用作操作栏项目上的实际可绘制图层。它的背景(写在上面)上面覆盖着扳手图标。
res/drawable/layer_customizer.xml
用于更改颜色的 Java 代码。目标视图是分配了layer_customizer可绘制对象的对象。传入的颜色将更改 shape_notification.xml 的实体标记颜色。
使用这些图层创建布局。
res/layout/actionview_customizer.xml
要获取要放入 ActionBar 中的自定义布局,请添加此菜单项:
res/menu/actionbar_main.xml
然后在加载操作栏后,使用此代码获取按钮的句柄。这发生在您的活动中。
如果您想查看完整的源代码,请查看我使用的应用程序源代码。http://code.google.com/p/motivatormaker-android/source/browse/MakeMotivator/src/com/futonredemption/makemotivator/activities/MainActivity.java
I solved this by doing the following.
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
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
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.
Create a layout using these layers.
res/layout/actionview_customizer.xml
To get the custom layout to put into the ActionBar add this menu item into it:
res/menu/actionbar_main.xml
Then after loading the Action Bar use this code to get the handle to the button. This happens in your Activity.
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