PopupMenu的演示样例
Posted cynchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PopupMenu的演示样例相关的知识,希望对你有一定的参考价值。
弹出菜单是停靠在一个View上的一个模式菜单。
假设View对象下方有空间,那么弹出菜单将显示在停靠对象的下方,否则会显示在上方。
这是很实用的:
源代码地址:http://download.csdn.net/detail/u013922681/8951537
文章引用:
php?
mod=viewthread&tid=83489">1.Android 关于PopupMenu的演示样例
2.Android应用中菜单(Menu)的位置显示问题
源代码:
MainActivity.java
package com.example.popMenu;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showPopup(View view) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(MainActivity.this, "click", 0).show();
break;
default:
break;
}
return false;
}
});
popupMenu.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(MainActivity.this, "click", 0).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="96dp"
android:layout_marginTop="111dp"
android:text="@string/hello_world"
android:onClick="showPopup"/>
</RelativeLayout>
效果
以上是关于PopupMenu的演示样例的主要内容,如果未能解决你的问题,请参考以下文章