android - 自定义通知 View

标签 android custom-view statelistdrawable

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

最好的方法是什么?我更喜欢圆角,并且可以选择在内部放置一个可绘制对象。此自定义 View 也将放置在操作栏中。我仍然需要 View 来响应 android:background state list drawables 这样我就可以让点击和选择符合工作。

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

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

最佳答案

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

创建这个是为了使圆角形状具有纯色。这也增加了一种半透明的黑色,使它在黑色背景下看起来更加紧致。 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>

图层可绘制对象将用作操作栏项目上的实际可绘制对象。它的背景(如上所示)覆盖了 Spanner 图标。 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代码。目标 View 是分配了 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"
   />

然后在加载 Action Bar 之后使用此代码获取按钮的句柄。这发生在您的 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);
        }
    });
}

如果您想查看完整的源代码,请查看我在其中使用的应用程序源代码。 http://code.google.com/p/motivatormaker-android/source/browse/MakeMotivator/src/com/futonredemption/makemotivator/activities/MainActivity.java

关于android - 自定义通知 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9033367/

相关文章:

android - 从代码而非 xml 创建选择器时出现意外结果

android - 按下时按钮背景更改布局

android - 即使有 Activity 客户端也会调用 Service.onUnbind() 吗?

xcode - 如何以编程方式更改自定义 View 对象的成员变量?

ios - 将参数从 Storyboard传递到自定义 UIView iOS

android - 使用自定义 View 创建小部件

android - 从新的 Google+(加号)照片应用程序下载图片

android - 如何通过点击选项菜单获取contextview

android - 如何使用 ShowcaseView android 库更改标题字体

android - 为什么当我触摸 ImageView 时,应用于 ImageView 的状态列表可绘制对象不使用按下状态的可绘制对象?