如何添加一个可点击的"活动及QUOT;行动项目actionBarSherlock? [英] How to add a clickable "events" action item to actionBarSherlock?
问题描述
许多应用程序(包括的谷歌加上 和 Facebook的 )都显示应用事件(或称通知)的数量动作栏项目。
many apps (including google plus and facebook) have an action bar item showing the number of in-app-events (or "notifications").
该操作项里面出现一个号码,你可以单击它,以显示应用程序有用户的事件。
This action item has a number within it and you can click it in order to show the events the app has for the user.
类似的东西(从<一所href=\"https://lh3.ggpht.com/vHPn_5FJEUOrXCyLC2_zQfNioSP9iL64V8c2b9LRiYK5nd2iOaaLRCEIZ4Qs5BuN8VM=h900-rw\"相对=nofollow> 这里 ):
something like that (taken from here) :
我要让它在旧的Android版本的工作,所以我用actionBarSherlock。
I wish to make it work on old android versions, so i use actionBarSherlock.
不幸的是,每一个解决方案,我用有它的缺点,我不能在这里(在计算器上)找到任何解决方案,与actionBarSherlock处理这(找到其他的解决方案,但与此库)。
Sadly, each solution i use has its disadvantages, and i couldn't find any solution here (on stackOverflow) that handles this with actionBarSherlock (found other solutions, but not with this library).
我还发现一个帖子它( 这里 一>),声称这是对这个库的一个问题,但它是很旧,似乎被关闭并标记为固定的,但我不能找出如何现在使用它。
I've also found a post about it (here) , claiming it's an issue on this library, but it's very old and seems to be closed and marked as fixed, but I can't find out how to use it now.
我试过在未来的解决方案:
i've tried the next solutions:
- actionLayout。它表现很好,但点击它没有显示点击效果。
- actionViewClass - 它甚至没有出于某种原因 。
- 添加菜单项,其视图编程。
什么是实现这一目标的最佳途径?
What's the best way to achieve this ?
编辑:这是我所使用actionLayout尝试:
this is what i've tried using actionLayout :
action_item_notification.xml - 现在它是同为abs__action_menu_item_layout.xml(<一href=\"https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock/res/layout/abs__action_menu_item_layout.xml\"相对=nofollow> 这里 )。以后我会添加一个TextView持有通知的数量。
"action_item_notification.xml" - for now it's the same as "abs__action_menu_item_layout.xml" (here). later i will add a textView to hold the number of notifications.
在菜单xml文件,我有此作为项目之一:
in the menu xml file, i have this as one of the items:
<item
android:id="@+id/activity_main__menuItem_notifications"
android:actionLayout="@layout/action_item_notification"
android:icon="@drawable/notification_button"
android:showAsAction="always"
android:title="@string/notifications"/>
不仅不显示图标,但在长期的项目点击会崩溃的应用程序,与上ActionMenuItemView.java文件中的NPE。
not only it doesn't show the icon, but long clicking on the item will crash the app, with a NPE on the ActionMenuItemView.java file.
编辑:好了,我已经找到了一个解决方案,几乎是完美的。
ok, so i've found a solution that is almost perfect.
它显示了行动项目很好,它甚至反应,点击与其他行动项目。
it shows the action item nicely and it even reacts to clicking as the other action items.
我可悲的是有一个缺失的功能 - 在操作项长点击以显示其标题的祝酒辞。可悲的是,我无法找到一个方法来克服这个,所以我做了什么(的作品)正在处理的视图本身长点击,并呼吁有类似code是用于 ActionMenuItemView :: onLongClick 。
I've sadly had one missing feature - long clicking on action item to show the toast of its title. sadly, i couldn't find a way to overcome this so what i did (that works) is handling the long clicking on the view itself, and call a similar code that is used for ActionMenuItemView::onLongClick .
如果任何人有一个更好的和更好的解决方案,请把它写下来。
if anyone has a better and nicer solution, please write it down.
我已经写在这里新的答案这一解决方案。
i've written this solution in a new answer here.
推荐答案
这是我的解决方案,但它是一个有点乱,并呼吁显示了操作项为actionBarSherlock之一举杯同code。
here's my solution, but it's a bit messy and calls the same code of showing a toast for the action item as the one of actionBarSherlock.
如果任何人有一个更好,更清洁的解决方案,请把它写下来。
if anyone has a better, cleaner solution, please write it down.
菜单文件(activity_main.xml中):
menu file (activity_main.xml) :
...
<item
android:id="@+id/activity_main__menuItem_notifications"
android:showAsAction="always"
android:title="@string/notifications"/>
...
MainActivity.java:
MainActivity.java :
public boolean onCreateOptionsMenu(...){
...
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
//
final MenuItem notificationsMenuItem = menu.findItem(R.id.activity_main__menuItem_notifications);
notificationsMenuItem.setActionView(R.layout.action_item_notification);
setEnableLongClickOnCustomActionItem(notificationsMenuItem,true);
...
public static void setEnableLongClickOnCustomActionItem(final MenuItem menuItem, final boolean enable) {
final View actionView = menuItem.getActionView();
if (actionView == null)
return;
final CharSequence title = menuItem.getTitle();
if (!enable || Strings.isEmpty(title))
actionView.setOnLongClickListener(null);
actionView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
final int[] screenPos = new int[2];
final Rect displayFrame = new Rect();
actionView.getLocationOnScreen(screenPos);
actionView.getWindowVisibleDisplayFrame(displayFrame);
final Context context = actionView.getContext();
final int width = actionView.getWidth();
final int height = actionView.getHeight();
final int midy = screenPos[1] + height / 2;
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
final Toast cheatSheet = Toast.makeText(context, title, Toast.LENGTH_SHORT);
if (midy < displayFrame.height()) {
// Show along the top; follow action buttons
cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT, screenWidth - screenPos[0] - width / 2, height);
} else {
// Show along the bottom center
cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
}
cheatSheet.show();
return true;
}
});
的行动项目(action_item_notification.xml)的布局文件:
layout file of the action item ( action_item_notification.xml) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="?attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:addStatesFromChildren="true"
android:focusable="true" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="@null"
android:focusable="false"
android:scaleType="fitCenter"
android:src="@drawable/notification_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignRight="@+id/imageView1"
android:background="@drawable/action_item_notification_counter_background"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:text="88"
android:textColor="#FFffffff"
tools:ignore="HardcodedText" />
</RelativeLayout>
和一个漂亮的绘制为TextView的(action_item_notification_counter_background.xml)的背景:
and a nice drawable for the background of the textView ("action_item_notification_counter_background.xml") :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#FFff0000" />
</shape>
这篇关于如何添加一个可点击的&QUOT;活动及QUOT;行动项目actionBarSherlock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!