Панель действий - отсутствует меню - PullRequest
0 голосов
/ 30 июня 2018

Поскольку я новичок в Android Studio, у меня есть один вопрос, и я надеюсь, что вы, ребята, не возражаете.

Итак, я создал меню действий, которое должно иметь 3 точки в правом верхнем углу и которое должно вызывать меню. В любом случае вот мое меню:

enter image description here

В своей основной деятельности я вижу меню, но не вижу заголовок и 3 точки.

Код моего класса main.java (LocationInit):

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())
        {
            case R.id.GoogleMaps:
                Intent googlemaps = new Intent(this, googlemaps.class);
                startActivity(googlemaps);
                break;
            case R.id.Settings:
                Intent settings = new Intent(this, SettingsActivity.class);
                startActivity(settings);
                break;
            case R.id.LastLocation:
                Toast.makeText(getApplicationContext(),"Help", Toast.LENGTH_LONG).show();
                break;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

AndroidManifest.xml:

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

Наконец, мой app_bar.xml (панель XML действий):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:theme="@style/CustomToolbarStyle"
    app:popupTheme="@style/CustomPopupTheme">



</android.support.v7.widget.Toolbar>

enter image description here

1 Ответ

0 голосов
/ 30 июня 2018

main_menu.xml код:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_main_setting"
        android:icon="@drawable/ic_settings"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:actionLayout="@layout/toolbar"
        android:title="Setting" />

    <item
        android:id="@+id/menu_main_setting2"
        android:icon="@drawable/ic_settings"
        android:orderInCategory="200"
        app:showAsAction="always"
        android:actionLayout="@layout/toolbar"
        android:title="Setting" />

</menu>

и просто переопределите onCreateOptionsMenu как это в вашем MainPage.java

@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;
}

и в onCreate () добавить это

toolbar.inflateMenu(R.menu.main_manu);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...