Android中自定义Menu
在Android中,默认的Menu菜单一行中最多只能显示3个菜单选项,如果我们想在Menu菜单一行中想显示多于3个菜单选项时,我们可以考虑采用PopWindow结合自定义Layout布局文件来创建自定义菜单。
首先,先在布局文件夹layout下面添加一个布局文件,命名为:popup_menu.xml。该文件的代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:background="@drawable/editbox_background_normal"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:layout_margin="6dip"> <Button android:id="@+id/btnClear" android:layout_marginTop="2dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/sms_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除账号" android:textColor="#ff222222"/> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:layout_margin="6dip"> <Button android:id="@+id/btnSet" android:layout_marginTop="2dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/sms_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网络设置" android:textColor="#ff222222"/> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:layout_margin="6dip"> <Button android:id="@+id/btnHelp" android:layout_marginTop="2dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/sms_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="帮助更新" android:textColor="#ff222222"/> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:layout_margin="6dip"> <Button android:id="@+id/btnExit" android:layout_marginTop="2dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/sms_icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出程序" android:textColor="#ff222222"/> </LinearLayout> </LinearLayout> </LinearLayout>在TestActivity主界面的窗体的代码如下:
public class TestActivity extends Activity { View view; PopupWindow pop; Button clearAcc,netSet,helpUpdate,exitApp; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initPopupWindow(); setupViews(); } /** * 处理键盘事件 */ public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_MENU){ if(!pop.isShowing()){ pop.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0); }else{ pop.dismiss(); } return true; }else{ return super.onKeyDown(keyCode, event); } } /** * 初始话自定义Menu */ private void initPopupWindow(){ view = this.getLayoutInflater().inflate(R.layout.popup_menu, null); pop = new PopupWindow(view,ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); pop.setOutsideTouchable(true); } /** * 控件初始化 */ private void setupViews(){ clearAcc = (Button)view.findViewById(R.id.btnClear); netSet = (Button)view.findViewById(R.id.btnSet); helpUpdate = (Button)view.findViewById(R.id.btnHelp); exitApp = (Button)view.findViewById(R.id.btnExit); clearAcc.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(TestActivity.this, "清除账号", Toast.LENGTH_SHORT).show(); } }); netSet.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(TestActivity.this, "网络设置", Toast.LENGTH_SHORT).show(); } }); helpUpdate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(TestActivity.this, "帮助更新", Toast.LENGTH_SHORT).show(); } }); exitApp.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(TestActivity.this, "退出程序", Toast.LENGTH_SHORT).show(); } }); } }
当我们运行程序后,点击Menu按钮时显示的自定义菜单如下:
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28