我在我的 android应用程序中实现了操作栏sherlock.此应用程序具有购物车功能.操作栏上有一个图标,可将用户带到购物车.当物品被添加到购物车并从购物车中移除时,我需要动态更改该图标
以下是截图:
menu.xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/scanner" android:icon="@drawable/ic_nav_scanner" android:showAsAction="always" android:title="@string/scanner"/> <item android:id="@+id/cart" android:icon="@drawable/ic_nav_cart" android:showAsAction="always" android:title="@string/cart"/> <item android:id="@+id/a_More" android:icon="@drawable/ic_nav_more" android:showAsAction="always" android:title="@string/more"> <menu> <item android:id="@+id/location" android:icon="@drawable/ic_nav_dropdown_location" android:showAsAction="always" android:title="@string/location_action"/> <item android:id="@+id/fav" android:icon="@drawable/ic_nav_dropdown_favourite" android:showAsAction="always" android:title="@string/favourites"/> <item android:id="@+id/info" android:icon="@drawable/ic_nav_dropdown_information" android:title="@string/information"/> </menu> </item> </menu>
码:
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.aiwmain, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case R.id.scanner: //scanner clicked break; }
任何有助于动态更改操作栏上的购物车图标的帮助表示赞赏.
//编辑
我设法想出这个:
@Override public boolean onPrepareOptionsMenu(Menu menu) { DBManager db = DBManager.getSingletonInstance(); int cartItemCount = db.getCartItemCount(this); MenuItem cart = menu.findItem(R.id.cart); MenuItem scanner = menu.findItem(R.id.scanner); if(cartItemCount > 0){ //I want this item to be changed, I do not want to have seperate drawables for all numbers. cart.setIcon(R.drawable.arrow_left); } return super.onPrepareOptionsMenu(menu); }我想出了解决我问题的答案,我为CART菜单项设计了一个自定义布局:
badge.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="48dp" android:layout_height="fill_parent" android:clickable="true" style="@android:style/Widget.ActionButton" > <!-- Menu Item Image --> <ImageView android:id="@+id/img_view" android:layout_width="48dp" android:layout_height="fill_parent" android:clickable="true" android:background="@drawable/ic_nav_cart" /> <!-- Badge Count --> <TextView android:id="@+id/actionbar_notifcation_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="99" android:textColor="#9F2828" android:textSize="14sp" android:layout_toRightOf="@+id/img_view" android:background="@drawable/cart_circle"/> </RelativeLayout>
在我的menu.xml中,我以这种方式包含它:
<item android:id="@+id/cart" android:actionLayout="@layout/badge" android:icon="@drawable/ic_nav_cart" android:showAsAction="always" android:title="@string/cart"/>
然后在我的活动中我使用它像这样:
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.aiwmain, menu); //getting the count of cart items DBManager db = DBManager.getSingletonInstance(); int cartItemCount = db.getCartItemCount(this); badgeLayout = (RelativeLayout) menu.findItem(R.id.cart).getActionView(); menu.getItem(1).getActionView().setOnClickListener(this); TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview); tv.setText(""+cartItemCount); //do not show the count if count is 0 if(cartItemCount == 0){ tv.setVisibility(View.GONE); } return super.onCreateOptionsMenu(menu); }
上述方法位于BaseActivity中,所有其他活动都从该BaseActivity继承.因此,您无需在所有活动中包含此内容.
要处理CART ICON的onClick,请在所有活动中执行此操作:
public void onClick(View v) { // navigating to the cart details by checking the ids of menu item and // clicked layout. if (v.getId() == R.id.cart) { Intent order = new Intent(getApplicationContext(), CartDetailsActivity.class); startActivity(order); } }
参考:How to get text on an ActionBar Icon?