我几乎没有开发Android应用程序的经验。我的朋友让我为他的应用程序创建一个小功能。我所坚持的是为选项创建一个子菜单,即我有它,以便当点击一个按钮时会弹出一个菜单,其中包含一些选项,我需要它在单击特定选项时打开第二个菜单。我将在下面发布我的代码(包括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);
}
答案 0 :(得分:0)
您可以通过以下方式显示子菜单。我修改了你的代码并使用了硬编码常量。我希望这会对你有所帮助。
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);
}
}