- android
- android.accessibilityservice
- android.accounts
- android.content
- android.database.sqlite
- android.graphics
- android.location
- android.media
- android.net
- android.os
- android.text
- android.view
- android.view.inputmethod
- android.widget
- AbsListView
- AbsoluteLayout
- AbsSeekBar
- AbsSpinner
- AdapterView
- AnalogClock
- BaseAdapter
- BaseExpandableListAdapter
- Button
- CheckBox
- CheckedTextView
- Checkable
- Chronometer
- CompoundButton
- CursorAdapter
- CursorTreeAdapter
- DatePicker
- DialerFilter
- DigitalClock
- EditText
- Filter
- Filter.FilterListener
- Filter.FilterResults
- ExpandableListAdapter
- Filterable
- Gallery
- Gallery.LayoutParams
- GridView
- GridLayout
- RadioGroup
- ImageView
- HorizontalScrollView
- ImageButton
- ImageSwitcher
- FilterQueryProvider
- ListAdapter
- ListView
- MediaController
- QuickContactBadge
- RadioButton
- RatingBar
- RelativeLayout
- RemoteViews
- ResourceCursorAdapter
- ResourceCursorTreeAdapter
- Scroller
- ScrollView
- SearchView
- SeekBar
- SeekBar.OnSeekBarChangeListener
- SimpleAdapter
- SimpleCursorAdapter
- SimpleCursorTreeAdapter
- SimpleExpandableListAdapter
- SlidingDrawer
- Spinner
- SpinnerAdapter
- WrapperListAdapter
- TabHost
- TabHost.TabSpec
- TextView
- TimePicker
- Toast
- TableLayout
- TableRow
- TableRow.LayoutParams
- TabWidget
- TextSwitcher
- ToggleButton
- TwoLineListItem
- VideoView
- ViewAnimator
- ViewFlipper
- ViewSwitcher
- ZoomButtonsController
- ZoomButton
- ZoomControls
- dalvik.system
MenuInflater
MenuInflater
版本:Android 2.3 r1
结构
继承关系
public class MenuInflater extends Object
java.lang.Object
android.view.MenuInflater
子类及间接子类
直接子类
TabActivity
概述
这个类是用来实例化菜单 XML 文件成菜单对象。
由于性能的原因,由于程序创建时候就加载一些预处理 XML 文件,Menu 过多就造成很重的负担。因此,这是目前无法在运行时使用多于一个 XmlPullParser 的 xml 文件去使用 MenuInflater,它只能使用一个 XmlPullParser 返回的编译过的资源(R.某些文件)
构造函数
public MenuInflater (Context context)
构造填充(inflater) 一个菜单
参见
公共方法
public void inflate (int menuRes, Menu menu)
菜单层次从一个指定的 xml 资源去填充,如果有错误会抛掷 InflateException
。
参数
menuRes 要加载 XML 布局文件中的资源 ID(例如 R.menu.main_activity
)
menu 要填充的菜单,这些项目和子菜单就被添加到要填充菜单中
补充
文章精选
MenuInflater Android 菜单从 xml 创建方法
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论