android - 无法将自定义 header 应用于上下文菜单(android)

标签 android layout header android-contextmenu

嗨,我正在开发小型安卓应用程序。在我的应用程序中,我显示了一个上下文菜单。 一切正常,唯一的问题是我无法将标题 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/

相关文章:

Tomcat 和缓存控制 : Private, 和过期 : <date in past> - Where is this set?

android - MifareClassic 连接失败 NFC

java - 为应用程序中的子应用程序制作启动器屏幕的好方法?

android - 智能横幅容器 View 不会调整大小

Python 值错误 : Invalid header name b':authority

CSS - 相对于内容的页眉/页脚背景

android - 在 android.webkit.CookieManager 中存储 session cookie

android - 上下文菜单 onItemClick (ListView)

java - 使用 Grids java 格式化 JTable

android - fragment 中的 Gridview