自定义通知视图

发布于 2024-12-29 05:32:47 字数 352 浏览 1 评论 0原文

我想创建一个类似于 Google+ 应用程序通知的通知图标视图。不同之处在于我需要能够在运行时更改颜色,其中 Google+ 图标为灰色或红色,因此我假设它们正在使用 StateListDrawable。

对此最好的方法是什么?我更喜欢有圆角的剪角,并且可以选择在里面有一个可绘制的。此自定义视图也将放置在操作栏中。我仍然需要视图来响应 android:background state 列表可绘制对象,以便我可以让单击和选定的一致工作。

此自定义视图也将放置在操作栏中。

Google+ 应用程序,右上角的通知图标呈灰色,中间有一个 0。

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.

Google+ app with the notification icon in the upper right that's grayed out and has a 0 in the middle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2025-01-05 05:32:47

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

创建这个是为了使圆角形状具有纯色。这还添加了半透明的黑色,使其在黑底上看起来有压迫感。
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/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 代码。目标视图是分配了layer_customizer可绘制对象的对象。传入的颜色将更改 shape_notification.xml 的实体标记颜色。

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/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" />

要获取要放入 ActionBar 中的自定义布局,请添加此菜单项:
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"
   />

然后在加载操作栏后,使用此代码获取按钮的句柄。这发生在您的活动中。

@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);
        }
    });
}

如果您想查看完整的源代码,请查看我使用的应用程序源代码。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

<?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>

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 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();
}

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" />

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"
   />

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);
        }
    });
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文