編輯:關於Android編程
android:title="":設置菜單項的title android:icon="":設置菜單項的icon android:alphabeticShortcut="":字母快捷鍵 android:numericShortcut="":數字快捷鍵 android:checked="":設置菜單項是否被選中 android:checkable="":設置菜單項是否能選中 android:enabled="":設置菜單項是否可用 android:visible="":設置菜單項是否見 android:orderInCategory="1":設置菜單項順序 android:showAsAction="always|ifRoom|withText|never":設置菜單項在anctionbar中是否放入overflow
package com.example.menu; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /* * 創建選項菜單 * */ @Override public boolean onCreateOptionsMenu(Menu menu) { /*參數: * 1、組別:如果不分組的話就寫Menu.NONE, * * 2、Id: Android根據這個Id來確定不同的菜單 * * 3、順序:那個菜單現在在前面由這個參數的大小決定 * * 4、文本: 菜單的顯示文本 */ menu.add(Menu.NONE, Menu.FIRST + 1, 1, "刪除").setIcon( android.R.drawable.ic_menu_delete); menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon( android.R.drawable.ic_menu_edit); menu.add(Menu.NONE, Menu.FIRST + 3, 3, "幫助").setIcon( android.R.drawable.ic_menu_help); menu.add(Menu.NONE, Menu.FIRST + 4, 4, "添加").setIcon( android.R.drawable.ic_menu_add); menu.add(Menu.NONE, Menu.FIRST + 5, 5, "詳細").setIcon( android.R.drawable.ic_menu_info_details); menu.add(Menu.NONE, Menu.FIRST + 6, 6, "發送").setIcon( android.R.drawable.ic_menu_send); return true; } /* * 選項選擇監聽 * */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case Menu.FIRST+1: Toast.makeText(MainActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+2: Toast.makeText(MainActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+3: Toast.makeText(MainActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+4: Toast.makeText(MainActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+5: Toast.makeText(MainActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+6: Toast.makeText(MainActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } }
package com.example.menu; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public boolean onCreateOptionsMenu(Menu menu) { //通過xml定義的menu getMenuInflater().inflate(R.menu.option_menu,menu); //返回true設置的菜單才會生效,這裡super.onCreateOptionsMenu(menu)調用父類方法返回true return true; } /* * 選項選擇監聽 * */ @Override public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(MainActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); return super.onOptionsItemSelected(item); } }
package com.example.menu; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.ContextMenu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; /** * Created by elimy on 2016-09-01. */ public class ContextMenuActivity extends AppCompatActivity { ArrayListarrayList = new ArrayList (); ArrayAdapter adapter; ListView listView ; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.context_menu_activity); //初始化listview listView = (ListView) findViewById(R.id.context_menu_lv); //初始化數組 for (int i=0;i<10;i++){ arrayList.add("數據"+i); } //初始化adapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,arrayList); //設置適配器 listView.setAdapter(adapter); //注冊上下文菜單 registerForContextMenu(listView); } /* * 創建選項菜單方法 * */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //聲明並初始化menu布局加載器 MenuInflater inflater= getMenuInflater(); //適配加載定義的menu xml inflater.inflate(R.menu.context_menu,menu); } @Override public boolean onContextItemSelected(MenuItem item) { Toast.makeText(ContextMenuActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); return super.onContextItemSelected(item); } }
package com.example.menu; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.support.v7.internal.view.menu.MenuBuilder; import android.support.v7.internal.view.menu.MenuPopupHelper; import android.support.v7.widget.PopupMenu; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * Created by elimy on 2016-09-01. */ public class PopMenuActivity extends AppCompatActivity { PopupMenu popupMenu; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pop_menu_activity); //聲明並初始化 final ImageView imageView = (ImageView) findViewById(R.id.menu_iv); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //toolbar設置標題 toolbar.setTitle("彈出自定義菜單"); //設置title顏色 toolbar.setTitleTextAppearance(PopMenuActivity.this,R.style.Theme_Toolbar_Title); //設置顯示toolbar setSupportActionBar(toolbar); //為圖片設置監聽 imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //初始化PopupMenu popupMenu = new PopupMenu(PopMenuActivity.this,imageView); //通過menu布局加載器適配菜單 getMenuInflater().inflate(R.menu.pop_menu,popupMenu.getMenu()); try { //通過反射獲取PopupMenu類的mPopup成員變量 Field field = popupMenu.getClass().getDeclaredField("mPopup"); //設置為可接近,可訪問,因為mPopup在PopupMenu類中為私有屬性 field.setAccessible(true); //通過屬性get方法獲取屬性的類MenuPopupHelper的類對象 MenuPopupHelper menuPopupHelper = (MenuPopupHelper) field.get(popupMenu); //設置強制顯示icon menuPopupHelper.setForceShowIcon(true); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } //顯示popupMenu popupMenu.show(); //設置監聽器 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { Toast.makeText(PopMenuActivity.this,item.getTitle().toString(),Toast.LENGTH_SHORT).show(); return false; } }); } }); } }
package com.example.menu; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; /** * Created by elimy on 2016-09-02. */ public class SubMenuActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sub_menu_activity); } @Override public boolean onCreateOptionsMenu(Menu menu) { SubMenu share =menu.addSubMenu("分享"); SubMenu edit = menu.addSubMenu("編輯"); SubMenu file = menu.addSubMenu("文件"); share.add(0,Menu.FIRST,1,"分享到微信"); share.add(0,Menu.FIRST+1,2,"分享到微博"); //去掉子菜單頭部 //share.clearHeader(); //設置父選項圖標,需要強制顯示才能看到 //share.setIcon(R.drawable.share_24); //設置子選項頭部顯示圖標 //share.setHeaderIcon(R.drawable.share_24); edit.add(0,Menu.FIRST+2,0,"復制"); edit.add(0,Menu.FIRST+3,1,"剪切"); file.add(0,Menu.FIRST+4,1,"打開"); file.add(0,Menu.FIRST+5,2,"新建"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case Menu.FIRST: Toast.makeText(SubMenuActivity.this,"點擊了分享到微信",Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+1: Toast.makeText(SubMenuActivity.this,"點擊了分享到微博",Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+2: Toast.makeText(SubMenuActivity.this,"點擊了復制",Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+3: Toast.makeText(SubMenuActivity.this,"點擊了剪切",Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+4: Toast.makeText(SubMenuActivity.this,"點擊了打開",Toast.LENGTH_SHORT).show(); break; case Menu.FIRST+5: Toast.makeText(SubMenuActivity.this,"點擊了新建",Toast.LENGTH_SHORT).show(); break; } return true; } }
也許各位魅藍Note 3的機友們都有過這樣的遭遇,發現在使用文件管理器的時候修改不了外置卡的文件,這個情況其實是安卓系統的一個安全機制,當然對於喜歡倒騰的機
本篇主要是對 google推出的性能優化典范 進行一個通篇的整理… 主要在於一些具體的優化技巧、至於 60fps、掉幀、gc、內存抖動、阈值…
代碼中用的自定義常量 public static final int TYPE_Normal = 1; public static final int TYP
本文實例講述了Android編程之藍牙測試。分享給大家供大家參考。具體分析如下:一、軟件平台:win7 + eclipse + sdk二、設計思路:配合倒計時定時器實現藍