我希望我的 Android 应用程序能够显示类似菜单按钮的内容,但菜单会从图像视图中弹出。
问问题
2578 次
2 回答
3
您可以使用弹出窗口:
活动课:
公共类 MainActivity 扩展 Activity {
ImageView mOptionsImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOptionsImage = (ImageView) findViewById(R.id.imageView1);
mOptionsImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAsDropDown(mButton, 0, v.getHeight());
ListView list = (ListView) findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// SOME YOUR CODE
popupWindow.dismiss();
}
});
}
});
}
活动主.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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="34dp"
android:text="Button" />
</RelativeLayout>
和 popupWindow 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>
请注意:您将看不到任何结果,直到您的列表为空,在测试此代码之前添加一些按钮、文本视图或用项目填充列表。;)
于 2013-08-09T14:06:04.233 回答
1
ImageView imV = (ImageView) findViewById(R.id.imV); // if declared in XML
imV.setOnClickListener(imVListener);
View.OnClickListener imVListener = new View.OnClickListener() {
public void onClick(View v) {
//you can use QuickAction menu to pop up
}
};
以及QuickAction 菜单的链接。
于 2013-08-09T13:35:48.247 回答