我已经在Android中为Menus编写了一个代码,但它现在出现在我的活动中,这是我的代码
我这里只显示我的相关代码,
的Manifest.xml
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
Run Code Online (Sandbox Code Playgroud)
MainActivity.Java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
// Some action here
}
}
}
Run Code Online (Sandbox Code Playgroud)
main.xml中
<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="com.example.androidex1.MainActivity" >
<item android:id="@+id/item1"
android:title="one" />
<item android:id="@+id/item2"
android:title="two" />
<item android:id="@+id/item3"
android:title="three" />
<item android:id="@+id/item4"
android:title="four" />
</menu>
Run Code Online (Sandbox Code Playgroud)
以前试过的步骤
结果:
尝试但仍然没有出现菜单
结果:
菜单仍然没有出现.
结果:
从未添加该属性.
结果:
super.onCreateOptionsMenu(menu); is also added in the code but still the menu's don't appear.
Run Code Online (Sandbox Code Playgroud)
我的问题我只想看菜单而不是动作栏,可能是什么问题?我没有收到任何错误,主要问题是我无法看到菜单本身.
我不清楚,你看到你的动作栏并没有看到菜单,或者你根本看不到动作栏?如果它是最新的,请尝试<application>在清单中的标记中应用支持操作栏的样式.例如:
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
Run Code Online (Sandbox Code Playgroud)
小智 6
就我而言,我解决了这个问题
android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
Run Code Online (Sandbox Code Playgroud)
样式.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
Run Code Online (Sandbox Code Playgroud)
活动主文件
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:background="@color/colorPrimary"/>
Run Code Online (Sandbox Code Playgroud)
我尝试了你的代码,在我的手机上一切正常!唯一不同的想法是
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item1:
//DoSomething();
Toast.makeText(getApplicationContext(), "Hello 1", Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
//DoSomething2();
Toast.makeText(getApplicationContext(), "Hello 2", Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
//DoSomething3();
Toast.makeText(getApplicationContext(), "Hello 3", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
您确定目录和菜单名称正确吗?
getMenuInflater().inflate(R.menu.main, menu);
Run Code Online (Sandbox Code Playgroud)
确保目录是 menu 并且文件菜单是 main.xml 当它自动生成的 Android Studio 时,将其称为 main_activity_menu.xml,因此请确保充气器中的名称正确,因为我使用您的代码创建了一个新的测试应用程序,并且我看菜单!
如果您想添加带有图标等菜单项的 ActionBar,请阅读此Google.developer 指南
如果不需要快乐编码,来自 google.developer 的另一个菜单指南没有操作栏!
| 归档时间: |
|
| 查看次数: |
20856 次 |
| 最近记录: |