我wana在放置在操作欄中的購物車圖片上添加通知徽章並以編程方式對其進行操作。任何幫助?通知徽章上的行動項目Android
4
A
回答
38
您可以通過創建一個MenuItem
顯示custom layout
上ActionBar
定製MenuItem
。要設置自定義佈局,您必須使用菜單項屬性app:actionLayout
。
按照以下步驟在Cart
操作項目上創建Badge
。查看附件image
的結果。
- 創建
ImageView
自定義佈局(對於車圖標)和TextView
(對計數值)
佈局/ custom_action_item_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_action_cart"/>
<TextView
android:id="@+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="@drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="@android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
- 使用
Shape
創建可繪製的圓形badge
背景。 - 添加
custom layout
到菜單item
。 - 在你MainActivity,添加以下代碼:
抽拉/ badge_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@android:color/holo_red_dark"/>
<stroke android:color="@android:color/white" android:width="1dp"/>
</shape>
菜單/ main_menu.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_cart"
android:icon="@drawable/ic_action_cart"
android:title="Cart"
app:actionLayout="@layout/custom_action_item_layout"
app:showAsAction="always"/>
</menu>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
................
......................
TextView textCartItemCount;
int mCartItemCount = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.....................
............................
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.trash, menu);
final MenuItem menuItem = menu.findItem(R.id.action_cart);
View actionView = MenuItemCompat.getActionView(menuItem);
textCartItemCount = (TextView) actionView.findViewById(R.id.cart_badge);
setupBadge();
actionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(menuItem);
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_cart: {
// Do something
return true;
}
}
return super.onOptionsItemSelected(item);
}
private void setupBadge() {
if (textCartItemCount != null) {
if (mCartItemCount == 0) {
if (textCartItemCount.getVisibility() != View.GONE) {
textCartItemCount.setVisibility(View.GONE);
}
} else {
textCartItemCount.setText(String.valueOf(Math.min(mCartItemCount, 99)));
if (textCartItemCount.getVisibility() != View.VISIBLE) {
textCartItemCount.setVisibility(View.VISIBLE);
}
}
}
}
..................
..............................
}
OUTPUT:
0
最簡單的給予風格破解。
<TextView
android:id="@+id/fabCounter"
style="@style/Widget.Design.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:padding="5dp"
android:text="10"
android:textColor="@android:color/black"
android:textSize="14sp" />
0
也許這將是一個更快,更容易solution。 例如XML:
<ru.nikartm.support.ImageBadgeView
android:id="@+id/ibv_icon2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:padding="10dp"
app:ibv_badgeValue="100"
app:ibv_badgeTextSize="12sp"
app:ibv_fixedBadgeRadius="15dp"
app:ibv_badgeTextStyle="bold"
app:ibv_badgeTextColor="#ffffff"
app:ibv_badgeColor="#00ACC1"
app:ibv_badgeLimitValue="false"
android:src="@drawable/ic_shopping_cart" />
或以編程方式:
imageBadgeView.setBadgeValue(27)
.setBadgeOvalAfterFirst(true)
.setBadgeTextSize(16)
.setMaxBadgeValue(999)
.setBadgeTextFont(typeface)
.setBadgeBackground(getResources().getDrawable(R.drawable.rectangle_rounded))
.setBadgePosition(BadgePosition.BOTTOM_RIGHT)
.setBadgeTextStyle(Typeface.NORMAL)
.setShowCounter(true)
.setBadgePadding(4);
我希望這幫助。
相關問題
- 1. 在viewpager上添加通知徽章數
- 2. 推送通知徽章自動增量
- 3. iPhone像WPF項目中的紅色徽章通知?
- 4. 推送通知徽章
- 5. 圖標徽章通知
- 6. 工具欄上的抽屜徽章通知切換 - Android
- 7. NSTableView拖動項目「計數徽章」
- 8. 解除通知中心的通知後更新通知徽章
- 9. UITableView Cell中的通知徽章
- 10. Silverlight/xaml中的通知圖標徽章
- 11. Android - 在MenuItems上畫徽章
- 12. 推送通知 - 應用徽章圖標
- 13. 辭退引導徽章通知
- 14. 離子推送通知和徽章
- 15. 未讀/讀通知徽章在網頁
- 16. 推送通知:聲音和徽章
- 17. 徽章appStore通知不會更新
- 18. 推送通知徽章不會消失
- 19. 在我的應用程序上實施徽章通知
- 20. App圖標上的遠程通知徽章問題
- 21. 設置隱藏徽章選項 - > IOS 5.0通知
- 22. Android Layer-list徽章
- 23. 徽章在Android TabHost
- 24. 如何在Android中使用推送通知實現徽章
- 25. 在GridView中爲未讀通知添加徽章Android
- 26. 如何在android中使用徽章通知?
- 27. 使用Firebase在Android推送通知中設置徽章計數?
- 28. 如何在Android應用程序(如iPhone)上創建通知圖標徽章
- 29. 在AppDelegate的UITabBar項目上設置徽章值
- 30. UITableViewCell動作徽章
爲了「幫助」您,我們需要查看您已經編寫了哪些代碼,然後解釋什麼確切部分無法正確運行,然後解釋您在調查,調試和解決問題方面所做的工作,然後你可以解釋完成之後你完全有什麼困惑。 – csmckelvey
我已經在菜單下使用menu.xml添加購物車圖像,我是新手佈局設計,所以無法找出添加通知計數,如果有人添加東西到購物車或刪除項目。 –
@SubratPani請參閱下面的完整答案。 – FAT