码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-10-24 15:54:19      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   io   os   ar   java   

1.选项菜单和子菜单

bubuko.com,布布扣 bubuko.com,布布扣

bubuko.com,布布扣 bubuko.com,布布扣

MainActivity.java

bubuko.com,布布扣
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)来绑定组件

bubuko.com,布布扣

activity_main.xml

bubuko.com,布布扣
<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

bubuko.com,布布扣
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);

bubuko.com,布布扣
<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

bubuko.com,布布扣
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

bubuko.com,布布扣

xml文件就用上面的menu1.xml

 MainActivity.java

bubuko.com,布布扣
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

 

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

标签:android   style   blog   http   color   io   os   ar   java   

原文地址:http://www.cnblogs.com/weijj/p/4048293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!