【发布时间】:2017-08-29 22:04:27
【问题描述】:
我需要在操作栏中制作“购物车图标,如附图所示 图标上有文字,显示用户添加到购物车的商品数量”
【问题讨论】:
-
您有什么问题吗?
-
你有没有尝试或尝试过什么?
-
有人给我这个链接stackoverflow.com/questions/35009812/…它完全解决了我的问题
我需要在操作栏中制作“购物车图标,如附图所示 图标上有文字,显示用户添加到购物车的商品数量”
【问题讨论】:
鉴于该功能的条件,我认为您需要实现类似布局的东西:
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:layout_marginRight="10dp"
android:gravity="right|center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/main_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:srcCompat="@drawable/heart_icon" />
<ImageView
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="-20dp"
app:srcCompat="@drawable/number_icon" />
</LinearLayout>
【讨论】:
好的,那么您需要手动创建图标菜单栏才能完全控制它。
首先在activity.xml中
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<!-- set any fixed size for 'width' but DO NOT set wrap_content-->
<RelativeLayout
android:layout_width="48dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="[YOUR ICON]"/>
<!-- you can set any attributes you want (background-fixed position .. etc)
and you can also remove alignParentBottom , alignParentEnd ..
i just add it to be clear -->
<TextView
android:id="@+id/notification_text_view"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
如果您使用活动,则可以直接访问它们。如果您使用片段,则执行以下步骤。
在 Fragment 类中:为 image_icon 和 text_notification 添加设置器
private ImageView iconImageView;
private TextView textNotificationView;
public void setIconImageView(ImageView iconImageView){
this.iconImageView= iconImageView;
}
public void setTextNotificationView(TextView textNotificationView){
this.textNotificationView= textNotificationView;
}
那么您需要手动设置片段。 在活动类
if(savedInstanceState == null) { // for rotation
[FragmentClass] fragment = new [FragmentClass]();
fragment.setIconImageView( (ImageView)findViewById(R.id.child_list_search_edit_view))
fragment.setTextNotificationView( (TextView)findViewById(notification_text_view))
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, fragment, [Fragment TAG]).commit();
}else{
[FragmentClass] childActivityFragment =([FragmentClass]) getSupportFragmentManager()
.findFragmentByTag([Fragment TAG]);
fragment.setIconImageView( (ImageView)findViewById(R.id.child_list_search_edit_view))
fragment.setTextNotificationView( (TextView)findViewById(notification_text_view))
}
现在您可以根据需要添加通知。
【讨论】: