[英]Action bar sub menu not working with actionLayout
Hi I am developing an android application. 嗨,我正在开发一个Android应用程序。 In my application I am using ActionBarSherlock.
在我的应用程序中,我正在使用ActionBarSherlock。 I defined few menu items in the action-bar, in the following way:
我在动作栏中定义了几个菜单项,方法如下:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/card_menu"
android:title="cards"
android:showAsAction="always"
android:actionLayout="@layout/action_button"
>
<menu>
<item android:id="@+id/C1"
android:title="C1" />
<item android:id="@+id/C2"
android:title="c2" />
<item android:id="@+id/C3"
android:title="C3" />
</menu>
</item>
<item android:id="@+id/notification"
android:title="Notifications"
android:showAsAction="always"
android:actionLayout="@layout/notification_icon"
android:icon="@drawable/notification"
/>
<item android:id="@+id/filter"
android:icon="@drawable/filter"
android:title="Filter"
android:showAsAction="always"
/>
Now, everything displayed very well, but my problem is that when I click on a card_menu item where I define sub menus and also define an action layout; 现在,一切都显示得很好,但我的问题是,当我点击card_menu项目,我定义子菜单,并定义一个动作布局; it's not showing those sub menus.
它没有显示那些子菜单。
My other menu items are working properly. 我的其他菜单项工作正常。 Only when I define an action layout for my item which contains sub menus at that time I am not able to display the sub menu.
只有当我为包含子菜单的项目定义动作布局时,我才能显示子菜单。
If I remove the action layout, then it's working fine... 如果我删除动作布局,那么它工作正常......
I know if we define an action layout for an item, then we have to manually handle the click listener. 我知道如果我们为项目定义一个动作布局,那么我们必须手动处理点击监听器。 I did that the following way:
我这样做的方式如下:
final MenuItem item = menu.findItem(R.id.card_menu);
item.getActionView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(item);
Toast.makeText(getActivity(), "click on menu", Toast.LENGTH_SHORT).show();
}
});
I am able to handle the click event for that item, but not able to show drop-down sub menu items.. 我能够处理该项目的click事件,但无法显示下拉子菜单项。
How to solve this problem? 如何解决这个问题呢? How can I open my sub menus?
如何打开子菜单?
Need Help.... Thank you... 需要帮助....谢谢......
I had similar issue and solved it with a trick using a Spinner inside the Actionbar. 我有类似的问题,并使用Actionbar内的Spinner解决它。 So my layout of the actionbar in res/menu is (action_share is important):
所以我在res / menu中的操作栏布局是(action_share很重要):
<item android:id="@+id/action_share"
android:showAsAction="always"
android:actionLayout="@layout/actionbar_spinner_export" />
In my res/layout folder, I put in actionbar_spinner_export: 在我的res / layout文件夹中,我输入了actionbar_spinner_export:
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sp_export"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/actionbar_item_selector" />
Then in the onCreateOptionsMenu, I got this spinner and add a custom ArrayAdapter to this. 然后在onCreateOptionsMenu中,我得到了这个微调器,并为此添加了一个自定义ArrayAdapter。 I can only post the code in c#, since I am working with Xamarin.Android Cross Platform Development.
我只能在c#中发布代码,因为我正在使用Xamarin.Android跨平台开发。 But its nearly the same for Java:
但它与Java几乎相同:
IMenuItem spinnerExport = menu.FindItem(Resource.Id.action_share);
_sp_export = spinnerExport.ActionView.FindViewById<Spinner>(Resource.Id.sp_export);
_sp_export.Adapter = new ExportAdapter(_parent, Resource.Layout.actionbar_export_row, new List<String>{"Drucken", "Als PDF", "Als Text", "Als Tabelle"});
Then in my custom ExportAdapter, I put in getView the image, I want to show (in this case a share-icon). 然后在我的自定义ExportAdapter中,我将getView图像放入,我想显示(在这种情况下是一个共享图标)。 And in getDropDownView, I put all my items.
在getDropDownView中,我放了所有项目。 Here is the code:
这是代码:
class ExportAdapter : ArrayAdapter
{
private List<String> _objects = null;
private Context _context = null;
public ExportAdapter(Context context, int resourceId, List<String> objects) : base(context, resourceId)
{
_context = context;
_objects = objects;
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
convertView = ((Activity) _context).LayoutInflater.Inflate(Resource.Layout.actionbar_export_row, parent, false);
TextView tv_text = convertView.FindViewById<TextView>(Resource.Id.tv_text);
ImageView iv_image = convertView.FindViewById<ImageView>(Resource.Id.iv_image);
RelativeLayout.LayoutParams lp_iv = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);
lp_iv.AddRule(LayoutRules.CenterInParent);
iv_image.LayoutParameters = lp_iv;
iv_image.SetPadding(5,5,5,5);
tv_text.Visibility = ViewStates.Gone;
iv_image.SetImageResource(Resource.Drawable.ic_action_share);
return convertView;
}
public override View GetDropDownView (int position, View convertView, ViewGroup parent)
{
convertView = ((Activity) _context).LayoutInflater.Inflate(Resource.Layout.actionbar_export_row, parent, false);
TextView tv_text= convertView.FindViewById<TextView>(Resource.Id.tv_text);
tv_text.Text = _objects.ElementAt(position);
return convertView;
}
public override int Count {
get {
return _objects.Count();
}
}
public String getItemAtIndex(int position)
{
return _objects.ElementAt(position);
}
}
This way, I have an actionbar Icon, which looks like a real actionbar-item. 这样,我有一个动作栏图标,看起来像一个真正的动作栏项目。 And when I click on it, there will be the items in my spinner showing up.
当我点击它时,我的微调器中的物品会出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.