Verzeichnis suchen
android Manifest Manifest.permission Manifest.permission_group android.accessibilityservice AccessibilityService android.accounts Account android.app NotificationManager android.bluetooth BluetoothAdapter BluetoothClass BluetoothClass.Device BluetoothClass.Device.Major BluetoothClass.Service BluetoothDevice BluetoothServerSocket BluetoothSocket android.content SharedPreferences android.database.sqlite SQLiteCursorDriver SQLiteOpenHelper android.graphics Bitmap android.location LocationListener Geocoder GpsStatus GpsStatus.Listener GpsStatus.NmeaListener GpsSatellite android.media AudioFormat AsyncPlayer AudioRecord AudioRecord.OnRecordPositionUpdateListener ThumbnailUtils AudioManager android.net TrafficStats MailTo LocalSocket android.os AsyncTask AsyncTask.Status CountDownTimer Message MessageQueue HandlerThread android.text Html android.util JsonWriter android.view ContextMenu ContextMenu.ContextMenuInfo Display ViewManager View ViewStub ViewTreeObserver ViewParent WindowManager GestureDetector Gravity MenuInflater ScaleGestureDetector SoundEffectConstants android.view.inputmethod InputConnection InputMethod InputMethodSession BaseInputConnection InputMethodManager android.widget AbsListView AbsListView.LayoutParams AbsListView.OnScrollListener AbsListView.RecyclerListener AbsoluteLayout AbsoluteLayout.LayoutParams AbsSeekBar AbsSpinner AdapterView AdapterView.AdapterContextMenuInfo AdapterView.OnItemLongClickListener AdapterView.OnItemSelectedListener AdapterView.OnItemClickListener AnalogClock BaseAdapter BaseExpandableListAdapter Button CheckBox CheckedTextView Checkable Chronometer Chronometer.OnChronometerTickListener CompoundButton CompoundButton.OnCheckedChangeListener CursorAdapter CursorTreeAdapter DatePicker DatePicker.OnDateChangedListener DialerFilter DigitalClock EditText Filter Filter.FilterListener Filter.FilterResults ExpandableListAdapter ExpandableListView.OnChildClickListener ExpandableListView.OnGroupClickListener ExpandableListView.OnGroupCollapseListener ExpandableListView.OnGroupExpandListener Filterable Gallery Gallery.LayoutParams GridView GridLayout GridLayout.Alignment RadioGroup ImageView ImageView.ScaleType HorizontalScrollView ImageButton ImageSwitcher FilterQueryProvider ListAdapter ListView MediaController MultiAutoCompleteTextView MultiAutoCompleteTextView.CommaTokenizer MultiAutoCompleteTextView.Tokenizer QuickContactBadge RadioButton RatingBar RatingBar.OnRatingBarChangeListener RelativeLayout RemoteViews ResourceCursorAdapter ResourceCursorTreeAdapter Scroller ScrollView SearchView SearchView.OnCloseListener SearchView.OnQueryTextListener SearchView.OnSuggestionListener SeekBar SeekBar.OnSeekBarChangeListener SimpleAdapter SimpleAdapter.ViewBinder SimpleCursorAdapter SimpleCursorAdapter.CursorToStringConverter SimpleCursorAdapter.ViewBinder SimpleCursorTreeAdapter SimpleCursorTreeAdapter.ViewBinder SimpleExpandableListAdapter SlidingDrawer SlidingDrawer.OnDrawerCloseListener SlidingDrawer.OnDrawerOpenListener SlidingDrawer.OnDrawerScrollListener Spinner SpinnerAdapter WrapperListAdapter TabHost TabHost.TabSpec TextView TimePicker TimePicker.OnTimeChangedListener Toast TableLayout TableLayout.LayoutParams TableRow TableRow.LayoutParams TabWidget TextSwitcher ToggleButton TwoLineListItem VideoView ViewAnimator ViewFlipper ViewSwitcher ViewSwitcher.ViewFactory ZoomButtonsController ZoomButtonsController.OnZoomListener ZoomButton ZoomControls dalvik.system DexFile
Figuren


MenuInflater

版本:Android 2.3 r1

结构

继承关系

public class MenuInflater extends Object

        

java.lang.Object

android.view.MenuInflater

子类及间接子类

直接子类

TabActivity

 

概述

这个类是用来实例化菜单XML文件成菜单对象。

由于性能的原因,由于程序创建时候就加载一些预处理XML文件,Menu过多就造成很重的负担。因此,这是目前无法在运行时使用多于一个XmlPullParserxml文件去使用MenuInflater,它只能使用一个XmlPullParser返回的编译过的资源(R.某些文件)

 

构造函数

         public MenuInflater (Context context)

构造填充(inflater)一个菜单

参见

getMenuInflater()

 

公共方法

         public void inflate (int menuRes, Menu menu)

菜单层次从一个指定的xml资源去填充,如果有错误会抛掷InflateException

参数

menuRes         要加载XML布局文件中的资源ID(例如R.menu.main_activity

menu       要填充的菜单,这些项目和子菜单就被添加到要填充菜单中

补充

文章精选

MenuInflater Android菜单从xml创建方法

         AndroidMenuInflater实例

         Android MenuInflater的使用(布局定义菜单)

示例代码

新建一个android2.2的项目,项目文件列表

MenuInfalterTest.java

public class MenuInflaterTest extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

    }

 

    public boolean onCreateOptionsMenu(Menu menu) {

       // 获取当前的菜单

       MenuInflater inflater = getMenuInflater();

       // 填充菜单

       inflater.inflate(R.menu.option_menu, menu);

       return true;

    }

 

   

    public boolean onOptionsItemSelected(MenuItem item) {

       switch (item.getItemId()) {

       case R.id.menu_add:

           break;

       case R.id.menu_wallaper:

           break;

       case R.id.menu_search:

           break;

       case R.id.menu_setting:

           showSettings();

           break;

       }

       return super.onOptionsItemSelected(item);

    }

 

   

    private void showSettings() {

       Intent settings = new Intent

       (android.provider.Settings.ACTION_SETTINGS);

       settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK

              | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

       startActivity(settings);

    }

}

}

 

Main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />

</LinearLayout>

 

Option_menu.xml

<?xml version="1.0" encoding="utf-8"?> 

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:id="@+id/menu_add" 

        android:title="Add" 

        android:icon="@android:drawable/ic_menu_add"/> 

     <item android:id="@+id/menu_wallaper" 

        android:title="Wallpaper" 

        android:icon="@android:drawable/ic_menu_gallery"/> 

    <item  android:id="@+id/menu_search" 

        android:title="Search" 

        android:icon="@android:drawable/ic_search_category_default"/> 

    <item  android:id="@+id/menu_setting" 

        android:title="Settings" 

      android:icon="@android:drawable/ic_menu_preferences"/>                             </menu> 

 


Vorheriger Artikel: Nächster Artikel: