操作栏-缺少菜单

时间:2018-06-30 10:53:30

标签: java android android-studio

由于我是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 :(得分:0)

main_menu.xml 代码:

(* These are all from the standard library *)
Locate "{ _ : _ | _ }".
Print sig.
Print In.
Print fst.

(* Defining Property here to shorten code for exist *)
Definition P l (x : bool * nat) := fst x = true /\ In x l.

Fixpoint existbool_ex (l : list (bool * nat)) :
  option {x : bool * nat | fst x = true /\ In x l} :=
  match l return option {x : bool * nat | P l x} with
  | [] => None
  | x' :: l' =>
    match x' with
    | (true,n) as ans =>
      Some (exist (P (ans :: l')) ans (conj eq_refl (or_introl eq_refl)))
    | (false,n) =>
      match existbool_ex l' with
      | None => None
      | Some (exist _ x a) =>
        match a with
        | conj Heq Hin =>
          Some (exist (P ((false, n) :: l')) x (conj Heq (or_intror Hin)))
        end
      end
    end
  end.

(* Note the as pattern got desugared into a let binding. *)
Print existbool_ex.

(* However we have a somewhat sane extraction, (tail recursive) *)
Require Extraction.
Extraction existbool_ex.

,只需在MainPage.java中这样覆盖 onCreateOptionsMenu 即可

<?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>

并在 onCreate()中添加

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