在Android的菜单项
答
考虑到功能,我相信你需要实现一些类似的布局条件:
<?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>
答
okey然后你需要手动创建你的图标菜单栏以对其进行完全控制。
首先在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>
那么你可以,如果你使用的片段,然后做这些步骤直接访问他们,如果你使用的活动..
在片段类:两个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))
}
现在,只要你想,你可以添加您的通知。
+0
感谢我的问题,我已经尝试过这个解决方案并且对我很好用 https://stackoverflow.com/questions/35009812/how-to-add-badges-on-toolbar-menuitem-icons?fref=gc –
你有问题吗? –
你有没有尝试或尝试任何东西? –
有人给我这个链接 https://stackoverflow.com/questions/35009812/how-to-add-badges-on-toolbar-menuitem-icons?fref=gc 它解决所有 –