我试过像这样的Action Bar
我希望当我点击Actionbar的项目时它会打印一个Toast但是它没有尝试过很多次。
我创建了2个自定义布局,一个用于通知自定义项目,另一个用于任务自定义项目。
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/noti"/>
<TextView
android:id="@+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginTop="3dp"
android:layout_marginLeft="15dp"
android:background="@drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="@android:color/white"
android:text="0"
android:textSize="10sp"/>
</RelativeLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/task"
android:id="@+id/imageView5" />
<TextView
android:id="@+id/cart_badge1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_gravity="right|end|top"
android:background="@drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="@android:color/white"
android:text="0"
android:textSize="10sp"
android:layout_alignTop="@+id/imageView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
navigation.menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:apps="http://schemas.android.com/tools">
<item
android:id="@+id/action_refresh"
android:icon="@drawable/noti"
app:showAsAction="always"
android:actionLayout="@layout/custom_action_notification_layout"
android:title="Refresh"/>
<item
android:id="@+id/action_task"
android:orderInCategory="100"
android:actionLayout="@layout/custom_action_task_layout"
android:title="Task"
android:icon="@drawable/task"
app:showAsAction="always" /></menu>
现在在我的navigationdrawer.class中,我把这个代码放在了创建OptionsMenu()上,获取了通知和任务项的ActionView以及将OnClick侦听器设置为那些ActionView。
navigation.class
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_refresh);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
// RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
final MenuItem itemNotification1 = menu.findItem(R.id.action_task);
MenuItemCompat.setActionView(itemNotification1, R.layout.custom_action_task_layout);
View actionViewNotification1 = MenuItemCompat.getActionView(itemNotification1);
// RelativeLayout notifCount1 = (RelativeLayout) MenuItemCompat.getActionView(itemNotification1);
actionViewNotification1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification1);
}
});
现在终于在onOptionsItemSelected()中代码就像
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
在许多方面尝试过,但根本没有工作,请从这里帮忙..
答案 0 :(得分:1)
1。创建两个custom layout
,一个用于Notification
自定义item
,另一个用于Task
自定义item
。
<强> custom_action_notification_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/icon_notification"/>
<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>
<强> custom_action_task_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/icon_task"/>
<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>
2。创建包含menu
和Notification
项的Task
XML。使用属性app:actionLayout
将custom layout
设置为每个item
。
<强> custom_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_notification"
android:icon="@drawable/icon_notification"
android:title="Notification"
app:actionLayout="@layout/custom_action_notification_layout"
app:showAsAction="always"/>
<item
android:id="@+id/action_task"
android:icon="@drawable/icon_task"
android:title="Task"
app:actionLayout="@layout/custom_action_task_layout"
app:showAsAction="always"/>
</menu>
3。在您的活动onCreateOptionsMenu()
中,获取ActionView
个Notification
项和Task
项,并将OnClick
个听众设置为ActionView
。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_notification);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
// Task
final MenuItem itemTask = menu.findItem(R.id.action_task);
View actionViewTask = MenuItemCompat.getActionView(itemTask);
actionViewTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(itemTask);
}
});
return true;
}
4. 最后,在onOptionsItemSelected()
中完成剩下的工作:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notification: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
仅供参考,我已通过显示Notification
消息检查了Task
和click
菜单项Toast
事件。
<强>输出:强>
<强>更新强>
在您更新的代码中使用:
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
而不是
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
希望这会有所帮助〜