主菜单的 XML 放在哪里?

Where to put main menu's XML?

你把main_menu.xml放在项目的什么地方?

我正在尝试制作一个操作栏。 如果 main_menu.xml 是操作栏的 xml, 在哪里存储操作栏的 xml? 注意:不重复!是的,有操作栏的例子,但我只是想知道,***我把 XML 文件放在哪里?*

你好@RedLight,这是定义操作栏的两种方式,一种是直接在你的 activity 中动态添加操作栏,或者其他方式是如果你想自定义你的操作栏,那么你可以使用工具栏并在操作中动态栏,下面是你可以看到的例子。

  1. 直接设置 Actiobar 或 Title(在您的 activity on Crate 内的广告代码)

对于动态操作栏,您需要使用如下操作栏定义 activity 的主题。

<activity
            android:name=".ActivitySplash"
            android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateAlwaysHidden"/>

在你的 activity 里面:

getSupportActionBar().setTitle("Home"); // Define here your actionbar title
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); // If you want to create home or back button inside actionbar
  1. 对于第二种类型,在 activity 顶部定义你的工具栏,并将它们定义为不像傻瓜一样的操作栏,

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_delete" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
    

主题喜欢

<activity
            android:name=".ActivitySplash"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateAlwaysHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

最后将工具栏动态设置为操作栏

Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar); 
        setSupportActionBar(toolbar);

对于菜单,您必须在 "res" 目录中创建菜单目录并将其放入该目录中,并在 activity.

中添加以下代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
   switch (item.getItemId()) 
   {
     case R.id.search:
      //your code here
        return true;
     default:
        return super.onOptionsItemSelected(item);
   }
}   

res 目录中的 menu 目录中。像

res/menu/some_menu.xml

如果您没有此目录,并且您正在使用 Android Studio,您可以右键单击 res 目录,然后单击 new,然后单击 Android Resource Directory .

在弹出的window中,Directory name写menu,Resource type写select menu,最后点击OK

在此目录中,您可以放置​​所有 menu.xml 文件。

PS:我必须再次回答这个问题,我有同样的问题,接受的答案更多是关于如何做工具栏而不是把菜单 xml 文件放在哪里。