自定义通知视图

Jer*_*rds 12 android custom-view statelistdrawable

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

对此最好的方法是什么?我更喜欢圆角的夹角,并且可以选择内部有一个可绘制的.此自定义视图也将放置在操作栏中.我仍然需要视图响应android:背景状态列表drawables所以我可以点击和选择按照工作.

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

Google+应用,右上角的通知图标显示为灰色,中间为0.

Jer*_*rds 23

我通过以下方式解决了这个问题.

创建它以使圆角形状具有纯色.这也增加了半透明的黑色,使其在黑色背景下显得紧致. RES /抽拉/ 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>
Run Code Online (Sandbox Code Playgroud)

图层drawable将用作操作栏项目上的实际drawable.它的背景(上面写着)覆盖着扳手图标. RES /抽拉/ 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>
Run Code Online (Sandbox Code Playgroud)

用于更改颜色的Java代码.目标视图是分配了layer_customizer drawable的对象.传入的颜色将更改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();
}
Run Code Online (Sandbox Code Playgroud)

使用这些图层创建布局. RES /布局/ 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" />
Run Code Online (Sandbox Code Playgroud)

要将自定义布局放入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"
   />
Run Code Online (Sandbox Code Playgroud)

然后在加载Action Bar后使用此代码获取按钮的句柄.这在您的活动中发生.

@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);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

如果你想看到完整的源代码一起工作来看看我在使用这个应用程序的源代码.http://code.google.com/p/motivatormaker-android/source/browse/MakeMotivator/src/com/futonredemption/makemotivator /activities/MainActivity.java