如何设置弹出菜单项的ID,以作为视图ID找到? [英] How to set id for popup menu items, to be found as view id?

查看:167
本文介绍了如何设置弹出菜单项的ID,以作为视图ID找到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动测试使用视图的ID来单击它们,因此我们会尽可能添加ID.

Out automatic tests use views' ids to be able to click on them, so we add ids whenever possible.

对于弹出菜单,有时需要动态填充它们,但是正如我所发现的,即使我确实为每个项目添加ID,也找不到ID,也无法使用该ID.即使使用DDMS的功能"UI自动器的转储视图层次结构",也显示弹出菜单中的任何视图都没有ID.

For popup menus, sometimes it's needed to populate them dynamically, but as I've found, even when I do add id for each item, the id isn't found, and can't be used. Even using DDMS's feature "dump view hierarchy for UI automator" shows that no view in the popup menu has an id.

这是我使用的示例代码,试图为单个菜单项设置ID.

This is a sample code of what I use, to try to set an id for the a single menu item.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View v=findViewById(R.id.button);
    v.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
            final Menu menu = popupMenu.getMenu();
            menu.add(0, R.id.myMenuItem, 0, R.string.app_name).setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(final MenuItem item) {
                    return false;
                }
            }) ;
            popupMenu.show();
        }
    });
}

请注意,id是在"ids.xml"文件中声明的,如下所示:

Note that the id is declared in "ids.xml" file, as such:

<item name="myMenuItem" type="id"/>

这就是DDMS工具向我展示的:

And this is what DDMS tool shows me :

为什么此代码无法按预期方式工作(意味着在菜单项视图中有一个ID)?我该怎么做才能使其中的视图具有ID?为动态创建的菜单项添加ID的正确方法是什么?

How come this code doesn't work as expected (meaning have an id for the menu item view) ? What can I do to make the views in it to have ids? What is the correct way to add ids for menu items that are created dynamically ?

推荐答案

好的,这绝不是问题中所描述问题的答案.将其视为替代PopupMenu来实现要求的替代方案.

Alright, this is by no means an answer to the problem described in the question. Look at it as an alternative to replace PopupMenu in order to achieve what was asked.

在仔细阅读了PopupMenu的文档及其源代码之后,我终于了解到PopupMenu并不是允许自定义的实现(我对注释中的误解向PO表示歉意).

After digging through the documents for PopupMenu and its source code, I finally come to understand that PopupMenu is not an implementation that would allow customization (my apology to the PO for the misconception in the comments).

作为替代方案,ListPopupWindow是创建弹出菜单的首选,其原因如下:

As an alternative, a ListPopupWindow is a preferred choice to create a popup menu with the following reasons:

  1. 它与PopupMenu-ListView共享同一父级.
  2. 它非常灵活,允许使用自定义布局定义自定义Adapter.
  3. 它还允许像PopupMenu一样运行时创建,并允许将资源ID附加到项目视图.
  1. It shares the same parent with PopupMenu - ListView.
  2. It is flexible, allowing custom Adapter to be defined with custom layout.
  3. It also allows run-time creation like PopupMenu does, and allows attaching resource id to the item view.

实施

首先,让我们为弹出项(popup_item_view.xml)定义一个自定义布局.

Implementation

First of all, let's define a custom layout for the popup item (popup_item_view.xml).

<?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"
    android:padding="16dp">
    <TextView
        android:id="@+id/popup_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

接下来,定义一个自定义的Adapter类以处理布局.

Next, define a custom Adapter class to manipulate the layout.

package com.example.popupmenu;

import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PopupAdapter extends BaseAdapter {
    public static class Item {
        public final int id;
        public final String title;

        public Item(int id, @NonNull String title) {
            this.id = id;
            this.title = title;
        }
    }

    private List<Item> mItemList = new ArrayList<>();

    public PopupAdapter(@NonNull Item[] items) {
        mItemList.addAll(Arrays.asList(items));
    }

    @Override
    public int getCount() {
        return mItemList.size();
    }

    @Override
    public Item getItem(int position) {
        return mItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Item item = getItem(position);
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.popup_item_view, parent, false);
        }
        convertView.setId(item.id);
        TextView titleView = (TextView) convertView.findViewById(R.id.popup_text);
        titleView.setText(item.title);
        return convertView;
    }
}

最后,将PopupMenu代码替换为此.

Finally, replace the PopupMenu code with this.

PopupAdapter.Item[] items = {
    new PopupAdapter.Item(R.id.popup_item_1, "item 1"),
    new PopupAdapter.Item(R.id.popup_item_2, "item 2")
};

ListPopupWindow popup = new ListPopupWindow(MainActivity.this);
popup.setAnchorView(view);
popup.setAdapter(new PopupAdapter(items));
popup.setModal(true);
popup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // do something
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
       // do something
    }
});
popup.show();

希望这会有所帮助.

这篇关于如何设置弹出菜单项的ID,以作为视图ID找到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆