安卓學習-界面-ui-普通菜單

1.選項菜單和子菜單java

 

 

MainActivity.javaandroid

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.上下文菜單ide

和上面的同樣,只不過這個菜單能夠綁定一個組件,建立完後必須調用registerForContextMenu(btn)來綁定組件字體

activity_main.xmlthis

<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.javaspa

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來配置菜單code

在menu目錄裏新建memu1.xmlxml

用下面語句調用blog

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

get

<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
相關文章
相關標籤/搜索