嗨,我正在开发小型安卓应用程序。在我的应用程序中,我显示了一个上下文菜单。 一切正常,唯一的问题是我无法将标题 View 设置到我的上下文菜单。 我的代码看起来像
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
LayoutInflater headerInflater = (LayoutInflater) getSherlockActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup header = (ViewGroup) headerInflater.inflate(
R.layout.context_menu_header, null);
menu.setHeaderView(header);
menu.setHeaderTitle("Edit Profile Pic");
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_camera, menu);
}
和我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/context_menu_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/actionbar"
/>
</LinearLayout>
它没有将该布局文件作为标题 View ...我还阅读了这个 Applying a custom header for ContextMenu
但这对我不起作用.. 需要帮助谢谢....
最佳答案
如果我在设置标题 View 后设置标题标题,则不会应用该 View 。 Insted 在 xml View 本身中设置标题标题,并在您应用标题 View 时设置标题。就像下面的方式..
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
LayoutInflater headerInflater = (LayoutInflater) getSherlockActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup header = (ViewGroup) headerInflater.inflate(
R.layout.context_menu_header, null);
// menu.setHeaderView(header);
TextView title = (TextView) header
.findViewById(R.id.header_textView);
title.setText("Edit Profile Pic");
menu.setHeaderView(header);
//menu.setHeaderTitle("Edit Profile Pic");
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_camera, menu);
}
它会正常工作。和这个问题一样Applying a custom header for ContextMenu .
谢谢...
关于android - 无法将自定义 header 应用于上下文菜单(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14602904/