步步为营_Android开发课[20]_用户界面之Menu(菜单)
Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305
- 主题:用户界面之Menu(菜单)
-
item元素的属性说明如下:
— id:唯一标示菜单的ID引用。
— menuCategory:菜单分类。
— orderInCategory:分类排序。
— title:菜单标题字符串。
— titleCondensed:浓缩标题,适合标题太长的时候使用。
— icon:菜单的图标。
— alphabeticShortcut:字符快捷键。
— numericShortcut:数字快捷键。
— checkable:是否可选。
— checked:是否已经被选。
— visible:是否可见。
— enabled:是否可用。
Menu默认样式(实例)
系统自带默认样式是在屏幕底部弹出一个菜单,这个菜单我们就叫他选项菜单OptionsMenu
res/menu下的menu_main.xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_settings"
android:orderInCategory="1"
android:title="设置"/>
<item
android:id="@+id/menu_about"
android:orderInCategory="2"
android:title="关于"/>
<item
android:id="@+id/menu_exit"
android:orderInCategory="3"
android:title="退出"/>
</menu>
MainActivity.java代码如下:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.memu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_about:
Toast.makeText(MainActivity.this, ""+"关于", Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings:
Toast.makeText(MainActivity.this, ""+"设置", Toast.LENGTH_SHORT).show();
break;
case R.id.action_exit:
Toast.makeText(MainActivity.this, ""+"退出", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
运行结果:
Menu自定义PopupWindow样式(实例)
自定义一个Menu布局,mymenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/zong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C1CDC1"
android:baselineAligned="false"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A1A1A1"
android:gravity="center_horizontal"
android:layout_marginTop="1dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/menu_0"
android:clickable="true"
android:layout_width="106dp" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center_horizontal"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dip"
android:layout_marginTop="1dp"
android:layout_gravity="fill_horizontal"
android:background="#00868B"
>
<ImageView
android:id="@+id/menu_item_iv_helper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myimage1"
/>
<TextView
android:id="@+id/menu_item_txt_helper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff222222"
android:text="关于"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/menu_1"
android:layout_width="106dp" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center_horizontal"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_gravity="fill_horizontal"
android:background="#00868B"
>
<ImageView
android:id="@+id/menu_item_iv_feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myimage2"
/>
<TextView
android:id="@+id/menu_item_txt_feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff222222"
android:text="设置"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/menu_2"
android:layout_width="106dp" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center_horizontal"
android:layout_marginTop="1dp"
android:layout_marginRight="1dp"
android:layout_gravity="fill_horizontal"
android:background="#00868B"
>
<ImageView
android:id="@+id/menu_item_iv_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myimage3"
/>
<TextView
android:id="@+id/menu_item_txt_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff222222"
android:text="退出"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
定义一个MyMenu类,MyMenu.java源代码:
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.PopupWindow;
public class MyMenu {
private PopupWindow pop;
private Activity activity;
private View[] menu = null;
public MyMenu(Activity activity){
this.activity = activity;
}
public PopupWindow getMenu(OnTouchListener touchListener,OnKeyListener keyListener){
View sub_view = activity.getLayoutInflater().inflate(R.layout.mymenu, null);
pop = new PopupWindow(sub_view,ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setAnimationStyle(R.style.PopupAnimation);
//pop.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.pop_menu_bg));
pop.setFocusable(true);
pop.setTouchable(true);
pop.setOutsideTouchable(true);
sub_view.setFocusableInTouchMode(true);
pop.setTouchInterceptor(touchListener);
sub_view.setOnKeyListener(keyListener);
return pop;
}
}
MainActivity.java源代码:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
private PopupWindow pop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyMenu myMenu = new MyMenu(this);
pop = myMenu.getMenu(touchListener, keyListener);
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_MENU){
if(pop.isShowing()){
pop.dismiss();
}
else{
pop.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0);
}
}
return super.onKeyDown(keyCode, event);
}
/**
* 处理外部点击事件,外部被点击,Menu就退出
*/
private OnTouchListener touchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
pop.dismiss();
}
return false;
}
};
private OnKeyListener keyListener = new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& keyCode == KeyEvent.KEYCODE_MENU) {
pop.dismiss();
return true;
}
return false;
}
};
}
运行结果:
Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。