Android菜单项,带有图标和此图标上的文字 [英] Android menu item with icon and text on this icon

查看:97
本文介绍了Android菜单项,带有图标和此图标上的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在操作栏中制作购物车图标,如附件中的图像所示 图标上带有文字,其中包含用户已添加到购物车中的商品数".

I need to make "a shopping cart icon in action bar like in attached image with text on icon with count of items which user added to cart"

推荐答案

okey,那么您需要手动创建图标菜单栏以对其进行完全控制.

okey then you need to create your icon menu bar manually to have full control on it.

首先在 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>

然后,如果您使用活动,则可以直接访问它们.如果您使用片段,则请执行以下步骤.

then you can access to them directly if you use activity .. if you use fragment then do these steps.

:为image_icon和text_notification添加设置器

in Fragment Class : add setter for both image_icon and text_notification

private ImageView iconImageView;
private TextView textNotificationView;


public void setIconImageView(ImageView iconImageView){
    this.iconImageView= iconImageView;
}
public void setTextNotificationView(TextView textNotificationView){
    this.textNotificationView= textNotificationView;
}

然后您需要手动设置片段. 在活动课程

then you need to set your fragment manually. In Activity Class

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))

    }

现在,您可以根据需要添加通知.

Now you can add your notifications as you want.

这篇关于Android菜单项,带有图标和此图标上的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆