如何使用锚定到按钮的自定义下拉菜单/弹出菜单?
我需要它像弹出菜单(锚定到视图)一样工作,并在我单击菜单中的项目时执行某些操作.
如何通过代码向菜单添加项目,保持菜单的高度,如果有超过5个项目,可以滚动它.我不需要添加任何图像,只需添加文本.

当然,我们在这里处理SDK 11及更高版本.
我打算做类似的事情:

旁边的每个项目在PopupMenu,我想将一个图标.
我创建了一个XML文件并将其放入/menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_one"
android:title="Sync"
android:icon="@android:drawable/ic_popup_sync"
/>
<item
android:id="@+id/action_two"
android:title="About"
android:icon="@android:drawable/ic_dialog_info"
/>
</menu>
Run Code Online (Sandbox Code Playgroud)
正如您所注意到的,在xml文件中我定义了我想要的图标,但是,当弹出菜单显示时,它显示的是没有图标的图标.我应该怎么做才能出现这两个图标?
我有一个ActionBar行动项目.单击操作项后,我想显示一个弹出菜单.我实现了这个方法,但是我希望将它锚定到操作项或者ActionBar布局中的任何视图.如何获得某种视图来锚定它MenuItem?
public boolean onOptionsItemSelected(MenuItem item) {
PopupMenu popupMenu = new PopupMenu(this, ??????); // What view goes here?
popupMenu.inflate(R.menu.counters_overflow);
popupMenu.show();
// ...
return true;
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个弹出菜单.我需要在条件下隐藏弹出菜单中的特定项目,我尝试了下面的代码,但它不起作用并显示"意外地你的应用程序已被停止".我用过findViewById和setEnabled(false).
有没有其他方法可以隐藏弹出菜单中的项目?请帮忙.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.addMessage_Action:
AddMessage();
break;
case R.id.dropMenuAction:
menuItemView = findViewById(R.id.dropMenuAction);
PopupMenu popup = new PopupMenu(this, menuItemView);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popupmenu_for_message_delete, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(this);
if(Global.lock == true)
findViewById(R.id.lock_message).setEnabled(false);
else
findViewById(R.id.unlock_message).setEnabled(false);
break;
}
return false;
}
Run Code Online (Sandbox Code Playgroud) 我需要更改popuo菜单的文本颜色,但我找不到任何方法可以做到这一点,我可以更改popmenu的背景而不是文本,我以这种方式编辑style.xml:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!-- API 14 theme customizations can go here. -->
<item name="popupMenuStyle">@style/MyPopupMenu</item>
<item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
</style>
<style name="MyPopupMenu" parent="@style/Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">#0F213F</item>
</style>
<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Base.Widget.PopupMenu.Small">
<item name="android:textColor">#ffffff</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Base.Widget.PopupMenu.Large">
<item name="android:textColor">#ffffff</item>
</style>
Run Code Online (Sandbox Code Playgroud)
哪里出错了?
我的listview有一个自定义适配器.适配器包含textview和图像按钮.单击图像按钮后,我实现了一个弹出菜单.一切都很好.但是当从弹出菜单中选择选项时,logcat显示单行消息"尝试完成输入事件但输入事件接收器已被处理"并且没有发生任何事情.
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resourceId) {
super(context, resourceId);
}
public MyAdapter(Context context, int resourceId, List<String> string) {
super(context, resourceId, string);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
v = inflater.inflate(R.layout.adapter_layout, null);
}
String str = getItem(position);
if(str != null) {
TextView textView = (TextView)v.findViewById(R.id.editText1);
textView.setText(str);
ImageButton button = (ImageButton)v.findViewById(R.id.imageButton1);
button.setOnClickListener(new Custom_Adapter_Button_Click_Listener(getItemId(position), getContext()));
}
return v;
}
} …Run Code Online (Sandbox Code Playgroud) 我正在动作栏中的弹出菜单.但是我坚持要在动作栏下面显示确切的(剪切到剪切).我正在放两个快照.
我的问题屏幕截图:
我想在操作栏下方的确切弹出菜单,如下面的截图
更正截图:
我的代码片段:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_filter"
android:icon="@drawable/ic_filter_white_18dp"
android:title="@string/action_filter"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_label"
android:icon="@drawable/ic_add_circle_outline_white_18dp"
android:title="@string/action_label"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />
Run Code Online (Sandbox Code Playgroud)
我想用下拉菜单创建按钮,比如ICS上ActionBar中的溢出菜单按钮.我有问题因为PopupMenu在android 2.x中没有.使用Spinner的第二种方式,但它不适合我,因为Spinner总是在我的自定义背景或我选择的项目上显示第一个项目.我该如何实现它?一般来说,我想要ContextMenu但很少和位置点击视图.谢谢.
它看起来像这样:

android popupmenu android-4.0-ice-cream-sandwich android-actionbar drop-down-menu
如何重置PopupMenu项目列表的最大宽度?
比如你在运行时向popupmenu添加一些TMenuItems:
item1: [xxxxxxxxxxxxxxxxxxx]
item2: [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
Run Code Online (Sandbox Code Playgroud)
菜单会自动调整大小以适合最大的项目.但是然后你做Items.Clear并添加一个新项目:
item1: [xxxxxxxxxxxx ]
Run Code Online (Sandbox Code Playgroud)
最终结果如此,标题后面有一个很大的空白区域.
除了重新创建popupmenu之外还有其他解决方法吗?
这里是重现这种异常的代码:
procedure TForm1.Button1Click(Sender: TObject);
var
t: TMenuItem;
begin
t := TMenuItem.Create(PopupMenu1);
t.Caption := 'largelargelargelargelargelarge';
PopupMenu1.Items.Add(t);
PopupMenu1.Popup(200, 200);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
t: TMenuItem;
begin
PopupMenu1.Items.Clear;
t := TMenuItem.Create(PopupMenu1);
t.Caption := 'short';
PopupMenu1.Items.Add(t);
PopupMenu1.Popup(200, 200);
end;
Run Code Online (Sandbox Code Playgroud) popupmenu ×10
android ×9
android-4.0-ice-cream-sandwich ×1
android-menu ×1
colors ×1
delphi ×1
delphi-xe7 ×1
google-play ×1
listview ×1
menu ×1
menuitem ×1