安卓学习-界面-ui-菜单

1.选项菜单和子菜单

 

 

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) {
        SubMenu m1=menu.addSubMenu("字体大小");
        m1.addSubMenu(0,1,0,"10号字体");
        m1.addSubMenu(0,2,0,"18号字体");
        SubMenu m2=menu.addSubMenu("颜色");
        m2.addSubMenu(0,3,0,"红色");
        m2.addSubMenu(0,4,0,"绿色");
        menu.add("普通菜单");
        MenuItem eItem=menu.add("退出");
        //直接绑定退出按钮
        eItem.setIntent(new Intent(MainActivity.this, ExitActivity.class));
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if(item.getItemId()==1){
            Toast.makeText(MainActivity.this, "选择了10号字体", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==2){
            Toast.makeText(MainActivity.this, "选择了18号字体", Toast.LENGTH_SHORT).show();
        }else if(item.getItemId()==3){
            Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==4){
            Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show();
        }
            
        
        return super.onMenuItemSelected(featureId, item);
    }
}
View Code

 2.上下文菜单

和上面的一样,只不过这个菜单可以绑定一个组件,创建完后必须调用registerForContextMenu(btn)来绑定组件

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="46dp"
        android:layout_marginTop="24dp"
        android:text="长按我出菜单" />

</RelativeLayout>
View Code

MainActivity.java

public class MainActivity extends Activity {

    Button btn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn=(Button)findViewById(R.id.button1);
        registerForContextMenu(btn);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        SubMenu m1=menu.addSubMenu("字体大小");
        m1.addSubMenu(0,1,0,"10号字体");
        m1.addSubMenu(0,2,0,"18号字体");
        SubMenu m2=menu.addSubMenu("颜色");
        m2.addSubMenu(0,3,0,"红色");
        m2.addSubMenu(0,4,0,"绿色");
        menu.add("普通菜单");
        MenuItem eItem=menu.add("退出");
        //直接绑定退出按钮
        eItem.setIntent(new Intent(MainActivity.this, ExitActivity.class));
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getItemId()==1){
            Toast.makeText(MainActivity.this, "选择了10号字体", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==2){
            Toast.makeText(MainActivity.this, "选择了18号字体", Toast.LENGTH_SHORT).show();
        }else if(item.getItemId()==3){
            Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==4){
            Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show();
        }
        return super.onContextItemSelected(item);
    }

}
View Code

 3.采用XML来配置菜单

在menu目录里新建memu1.xml

用下面语句调用

MenuInflater m=new MenuInflater(this);
m.inflate(R.menu.memu1, menu);

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="字体大小">
        <menu>
            <item android:title="10号字体" android:id="@+id/item1"/>
            <item android:title="18号字体" android:id="@+id/item2"/>
                
        </menu>
    </item>
    <item android:title="颜色">
        <menu>
               <item android:title="红色"  android:id="@+id/item3"/>
            <item android:title="绿色"  android:id="@+id/item4"/>                
        </menu>
    </item>
    <item android:title="普通菜单"/>
    <item android:title="退出" android:id="@+id/item5"/>
</menu>
View Code

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) {
        MenuInflater m=new MenuInflater(this);
        m.inflate(R.menu.memu1, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        
        if(item.getItemId()==R.id.item1){
            Toast.makeText(MainActivity.this, "选择了10号字体", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==R.id.item2){
            Toast.makeText(MainActivity.this, "选择了18号字体", Toast.LENGTH_SHORT).show();
        }else if(item.getItemId()==R.id.item3){
            Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==R.id.item4){
            Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==R.id.item5){
            startActivity(new Intent(MainActivity.this,ExitActivity.class));
        }    
        
        return super.onMenuItemSelected(featureId, item);
    }
}
View Code

 

4.PopupMenu

xml文件就用上面的menu1.xml

 MainActivity.java

public class MainActivity extends Activity {

    Button btn1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                MenuInflater in=new MenuInflater(MainActivity.this);
                PopupMenu menu=new PopupMenu(MainActivity.this, btn1);
                in.inflate(R.menu.memu1, menu.getMenu());
                menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    
                    public boolean onMenuItemClick(MenuItem item) {
                         if(item.getItemId()==R.id.item1){
                                Toast.makeText(MainActivity.this, "选择了10号字体", Toast.LENGTH_SHORT).show();
                            }
                            else if(item.getItemId()==R.id.item2){
                                Toast.makeText(MainActivity.this, "选择了18号字体", Toast.LENGTH_SHORT).show();
                            }else if(item.getItemId()==R.id.item3){
                                Toast.makeText(MainActivity.this, "红色", Toast.LENGTH_SHORT).show();
                            }
                            else if(item.getItemId()==R.id.item4){
                                Toast.makeText(MainActivity.this, "绿色", Toast.LENGTH_SHORT).show();
                            }
                            else if(item.getItemId()==R.id.item5){
                                startActivity(new Intent(MainActivity.this,ExitActivity.class));
                            }    
                        return false;
                    }
                });
                
                menu.show();
            }
        });
        

        
        
    }


}
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。