我已经为Android中的菜单编写了代码,但它现在显示在我的活动中,以下是我的代码
我只是在这里显示我的相关代码,
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" >
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
}
}
}
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>
之前尝试过的步骤
Menu Items are not showing on Action Bar
结果:
已尝试,但仍未显示菜单
Android options menu not displaying
结果:
菜单仍未显示。
结果:
从未添加过该属性。
Option Menu does not appear in Android
结果:
super.onCreateOptionsMenu(menu); is also added in the code but still the menu's don't appear.
我的问题我只想看到菜单而不是操作栏,会有什么问题呢?我没有收到任何错误,主要问题是我不能看到菜单本身。
发布于 2016-03-07 22:23:52
我不清楚,你是看到了操作栏而没有看到菜单,还是根本看不到操作栏?如果是最新版本,请尝试在清单中的<application>
标签中应用支持操作栏的样式。例如:
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
发布于 2018-02-05 04:09:00
你必须从AppCompatActivity扩展,而不是Activity
发布于 2018-07-17 08:09:59
在我的案例中,我通过
android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
style.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
activity_main.xml
<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"/>
https://stackoverflow.com/questions/35845887
复制相似问题