开发者

android how to get menu like below image

开发者 https://www.devze.com 2023-04-02 19:32 出处:网络
In my activity there is an option menu, for an item there is a submenu. I want for a submenu item below will come

In my activity there is an option menu, for an item there is a submenu. I want for a submenu item below will come

android how to get menu like below image

i have xml like this,

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/file"
          android:icon="@drawable/file"
          android:title="@string/file" >
        <!-- "file" submenu -->
        <menu>
            <item android:id="@+id/create_new"
                  android:title="@string/create_new" />
            <item android:id="@+id/open"
                  android:title="@string/open" />
        </menu>
    </item>
</menu>

I know for option menu it is po开发者_如何转开发ssible but i want to put for a submenu item. How can i do that? Is there any other way to do this?


You should display your own dialog isntead of using menu:

http://developer.android.com/guide/topics/ui/dialogs.html

android how to get menu like below image

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();


You can override the menu button from onkeyDown() method and can show this dialog show it will be displayed as a menu itself.....!


The code will be like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    //return super.onKeyDown(keyCode, event);
    if(keyCode==KeyEvent.KEYCODE_MENU){
        // now create your dialog here
        return true;
    }
    return false;
}


You can use spinner:

String na[] = new String[namelist.size()];
Spinner spinname =(Spinner)findViewById(R.id.networkname); 
ArrayAdapter<String> adapter=new ArrayAdapter<String>this,android.R.layout.simple_spinner_item,na);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);         
spinname.setAdapter(adapter);
0

精彩评论

暂无评论...
验证码 换一张
取 消