Ana*_*ain 6 notifications icons android android-layout
我想像购物车一样为Android制作一个图标计数器.我看到很多电子商务应用购物车图标数量增加.我展示了flipkart app snapshot.:-
小智 8
在我的解决方案中,每当新通知到达时,计数器将增加(在购物应用程序中观察到)
试试这个,它适用于我的MOTO e2.
确保您的API级别> 14
创建如下布局:
</RelativeLayout>
<ImageView
android:id="@+id/counterBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/unread_background" />
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="8sp"
android:layout_centerInParent="true"
android:textColor="#FFFFFF" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
在onCreateOptionMenu
,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.testAction);
menuItem.setIcon(buildCounterDrawable(count, R.drawable.ic_menu_gallery));
return true;
}
Run Code Online (Sandbox Code Playgroud)
现在,为Icon构建方法:
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.counterValuePanel);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.count);
textView.setText("" + count);
}
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
Run Code Online (Sandbox Code Playgroud)
你可以参考这里
只需更改
android:actionLayout="@layout/feed_update_count"
为
app:actionLayout="@layout/feed_update_count"
. 它会工作
归档时间: |
|
查看次数: |
23998 次 |
最近记录: |