gpt4 book ai didi

java - 在android中创建菜单/子菜单

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:29 25 4
gpt4 key购买 nike

我在开发 Android 应用程序方面几乎没有经验。我的 friend 要求我为他的应用程序创建一个小功能。我所坚持的是为选项创建一个子菜单,即我拥有它,这样当单击按钮时会弹出一个带有几个选项的菜单,并且当单击特定选项时我需要它打开第二个菜单。我将在下面发布我的代码(XML 和 Java),希望有人能帮助我。目前我的菜单是通过 Java 代码而不是 XML 代码显示的。我不确定这是否是不好的做法,但我会让任何可以帮助我的人告诉我是否是。提前致谢。

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.android.postvu.MainActivity">

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/background"
android:scaleType="centerCrop"
android:src="@drawable/grid" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="@string/image_text_editor"
android:textColor="@android:color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Image Text Editor" />


<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:onClick="myOnClickMethod"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.98" />

Java 代码(与单击按钮和显示菜单相关):

public void myOnClickMethod(View v) {
registerForContextMenu(v);
openContextMenu(v);
}

final int CONTEXT_MENU_VIEW = 1;
final int CONTEXT_MENU_EDIT = 2;
final int CONTEXT_MENU_ARCHIVE = 3;


@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Options");
menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Take Photo");
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Photo Album");
menu.add(Menu.NONE, CONTEXT_MENU_ARCHIVE, Menu.NONE, "Plain Image");
}

@Override
public boolean onContextItemSelected (MenuItem item) {
switch (item.getItemId()) {
case CONTEXT_MENU_VIEW: {
}
break;
case CONTEXT_MENU_EDIT: {
}
break;
case CONTEXT_MENU_ARCHIVE: {
}
break;
}

return super.onContextItemSelected(item);
}

最佳答案

您可以通过以下方式显示子菜单。我修改了您的代码并使用了硬编码常量。我希望这会对您有所帮助。

public class MainActivity extends AppCompatActivity {
private View menuAnchorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menuAnchorView = findViewById(R.id.imageView2);
menuAnchorView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myOnClickMethod(menuAnchorView);
}
});
}

public void myOnClickMethod(View v) {
registerForContextMenu(v);
openContextMenu(v);
}

final int CONTEXT_MENU_VIEW = 1;
final int CONTEXT_MENU_EDIT = 2;
final int CONTEXT_MENU_ARCHIVE = 3;


@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if(v.getTag() == null || !v.getTag().equals("menu_item")) {
menu.setHeaderTitle("Options");
menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Take Photo");
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Photo Album");
menu.add(Menu.NONE, CONTEXT_MENU_ARCHIVE, Menu.NONE, "Plain Image");
}
else {
menu.setHeaderTitle("Sub menu");
menu.add(Menu.NONE, 4, Menu.NONE, "Sub-view");
menu.add(Menu.NONE, 5, Menu.NONE, "Sub-view-2");
}
}

@Override
public boolean onContextItemSelected (MenuItem item) {
closeContextMenu();
switch (item.getItemId()) {
case CONTEXT_MENU_VIEW: {
menuAnchorView.setTag("menu_item");
openContextMenu(menuAnchorView);
}
break;
case CONTEXT_MENU_EDIT: {
}
break;
case CONTEXT_MENU_ARCHIVE: {
}
break;
case 4:
case 5: menuAnchorView.setTag(null);
break;
}

return super.onContextItemSelected(item);
}

}

关于java - 在android中创建菜单/子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45185150/

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