gpt4 book ai didi

android - Android 中的 float 上下文菜单

转载 作者:行者123 更新时间:2023-11-29 00:38:32 25 4
gpt4 key购买 nike

我试图在长按按钮时创建一个 float 的上下文菜单。我已经阅读了所有答案,但我仍然为此而发疯。这是我的代码:

R.menu.menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/MnuOpc1" android:title="Opcion1"
android:icon="@drawable/ic_launcher"></item>
<item android:id="@+id/MnuOpc2" android:title="Opcion2"
android:icon="@drawable/ic_launcher"></item>
<item android:id="@+id/MnuOpc3" android:title="Opcion3"
android:icon="@drawable/ic_launcher"></item>

</menu>

OnCreateContextMenu(...)

    @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
}

onContextItemSelected(...)

@Override
public boolean onContextItemSelected(MenuItem item) {
Log.v("Hello...","I got the switch");

switch (item.getItemId()) {
case R.id.MnuOpc1:
Log.v("Hello...","Option 1");
return true;
case R.id.MnuOpc2:
Log.v("Hello...","Option 2");
return true;
default:
return super.onContextItemSelected(item);
}

我认为上面的所有代码都是正确的,但是在调用 registerForContextMenu 时我遗漏了一些东西:

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
linear = (LinearLayout) findViewById(R.layout.main);
registerForContextMenu(linear);

我尝试使用 registerForContextMenu(getListView()); 但它不起作用,我从 Eclipse 收到错误。我究竟做错了什么?

最佳答案

您说您正在尝试为按钮创建上下文菜单,但在您的 onCreate() 中您将上下文菜单注册到 LinearLayout。虽然不知道您实际遇到了什么错误,但无法判断是否就是这样。

关于android - Android 中的 float 上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11109393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com