Split Action Bar - Creating menu after clicking an

2019-09-06 04:11发布

问题:

I want to create something like this :

So far what I've done :

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_layer:
        //I want to create my menu here
        break;

    case R.id.menu_direction:

        break;

    default:
        break;
    }
    return (true);
}`

It's my main menu.xml :

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_layer"
      android:icon="@drawable/ic_action_search"
      android:title="@string/menu_layers"
      android:showAsAction="ifRoom|withText" />
<item android:id="@+id/menu_direction"
      android:icon="@drawable/ic_action_search"
      android:title="@string/menu_directions"
      android:showAsAction="ifRoom|withText" />
</menu>

& It's the menu I want to create after clicking on menu_layer :

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/satView"
android:icon="@drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="@string/sat_View"/>
<item
android:id="@+id/streetView"
android:icon="@drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/street_View"/>  
</menu>

Can anyone please help me with this ? Thanks in advance.

回答1:

What you see in that Maps app is a custom dialog. You can indeed make your own, fully customized dialogs. Create a layout, just like your Activity layout (say custom_dialog.xml)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Hey!"
   />

   <ListView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
   />
</LinearLayout>

Put this file into projects /layout folder.

And put this code to Activity's onCreateDialog(int), where you create dialogs that you want to show.

Dialog dialog = new Dialog(getApplicationContext());
dialog.setContentView(R.layout.custom_dialog);

[... misc. initialization]

I tried to simply answer your question but there is plenty of source available and Android's API Guide fully explains how to create and customize Dialogs.