Android Api Demos登顶之路(四十一)Fragment-->Menu
来源:互联网 发布:pp2000软件 编辑:程序博客网 时间:2024/05/22 02:17
这个demo演示了如何在Fragment中设置菜单项
activity_main.xml
<LinearLayout 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" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <CheckBox android:id="@+id/checkbox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="show fragment1 menu"/> <CheckBox android:id="@+id/checkbox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="show fragment2 menu"/> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout></LinearLayout>
MainActivity
public class MainActivity extends Activity implements OnClickListener { private CheckBox checkBox1; private CheckBox checkBox2; private MenuFragment1 fragment1; private MenuFragment2 fragment2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox1=(CheckBox) findViewById(R.id.checkbox1); checkBox2=(CheckBox) findViewById(R.id.checkbox2); checkBox1.setOnClickListener(this); checkBox2.setOnClickListener(this); //准备fragment,将fragment添加进事务,等待处理 FragmentTransaction ft=getFragmentManager().beginTransaction(); fragment1=(MenuFragment1) getFragmentManager().findFragmentByTag("tag1"); if(fragment1==null){ fragment1=new MenuFragment1(); ft.add(fragment1, "tag1"); } fragment2=(MenuFragment2) getFragmentManager().findFragmentByTag("tag2"); if(fragment2==null){ fragment2=new MenuFragment2(); ft.add(R.id.frameLayout,fragment2, "tag2"); } ft.commit(); //更新复选框状态及显示 updateMenuVisiblity(); } private void updateMenuVisiblity() { FragmentTransaction ft=getFragmentManager().beginTransaction(); if(checkBox1.isChecked()){ ft.show(fragment1); }else{ ft.hide(fragment1); } if(checkBox2.isChecked()){ ft.show(fragment2); }else{ ft.hide(fragment2); } ft.commit(); } //处理需要保存时的状态 @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); updateMenuVisiblity(); } @Override public void onClick(View v) { updateMenuVisiblity(); } /* * 定义用作菜单的fragment,这里没有实现onCreatView方法,所以这个 * fragment没有UI,只有两个菜单项。如果需要UI可以实现onCreatView方法 */ public class MenuFragment1 extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置该fragment拥有操作菜单项 setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); //添加菜单,并设置显示模式 menu.add("Menu 1A").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); menu.add("Menu 1B").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } } public class MenuFragment2 extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置该fragment拥有操作菜单项 setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); //添加菜单,并设置显示模式 menu.add("Menu 2").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView tv=new TextView(getActivity()); tv.setText("这是Fragment2的UI"); tv.setTextColor(Color.WHITE); tv.setBackgroundColor(Color.BLACK); return tv; } }}
0 0
- Android Api Demos登顶之路(四十一)Fragment-->Menu
- Android Api Demos登顶之路(四十八)Menu
- Android Api Demos登顶之路(三十五)Fragment-->Argument
- Android Api Demos登顶之路(三十六)Fragment-->ContextMenu
- Android Api Demos登顶之路(三十七)Fragment-->Dialog
- Android Api Demos登顶之路(四十)Fragment-->Layout
- Android Api Demos登顶之路(三十五)Fragment-->Alert Dialog
- Android Api Demos登顶之路(三十六)Fragment-->Custom Animation
- Android Api Demos登顶之路(三十八)Fragment-->Dialog or Activity
- Android Api Demos登顶之路(三十九)Fragment-->show hide
- Android Api Demos登顶之路(四十二)Fragment-->Nesting Tabs
- Android Api Demos登顶之路(四十三)Fragment-->Retain Instance
- Android Api Demos登顶之路(二)
- Android Api Demos登顶之路(三)
- Android Api Demos登顶之路(四)
- Android Api Demos登顶之路(五)
- Android Api Demos登顶之路(六)
- Android Api Demos登顶之路(七)
- hdu 1061 Rightmost Digit
- BZOJ 4128 Matrix BSGS+矩阵求逆
- 英特尔快速存储技术
- everyDayEnglish
- 最简单也最难——如何获取到Android控件的高度
- Android Api Demos登顶之路(四十一)Fragment-->Menu
- Hive之insert into 和insert overwrite
- 模态视图-多视图应用
- hive转置
- Linux编程——文件 IO操作
- Caffe + Ubuntu 14.04 + CUDA 6.5
- Spring入门之环境搭建:
- java基础之----选择排序 冒泡排序
- HDU 5411(矩阵快速幂)