ListPopupWindow不遵守WRAP_CONTENT宽度规范

Kar*_*uri 37 user-interface android popupwindow

我正在尝试使用ListPopupWindow来显示一个字符串列表ArrayAdapter(最终这将是一个更复杂的自定义适配器).代码如下.如屏幕截图所示,结果ListPopupWindow似乎表现为内容宽度为零.它显示了正确数量的项目,这些项目仍然可以点击,并且点击成功生成了一个项目Toast,因此至少可以正常工作.

一个有趣的说明:我可以提供宽度(以像素为单位)popup.setWidth(...)而不是ListPopupWindow.WRAP_CONTENT它会显示一些内容,但这似乎非常不灵活.

如何ListPopupWindow包装其内容?

测试活动:

public class MainActivity extends Activity {

    private static final String[] STRINGS = {"Option1","Option2","Option3","Option4"};
    private View anchorView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setHomeButtonEnabled(true);
        setContentView(R.layout.activity_main);
        anchorView = findViewById(android.R.id.home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                showPopup();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showPopup() {
        ListPopupWindow popup = new ListPopupWindow(this);
        popup.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, STRINGS));
        popup.setAnchorView(anchorView);
        popup.setWidth(ListPopupWindow.WRAP_CONTENT);
        popup.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "Clicked item " + position, Toast.LENGTH_SHORT).show();
            }
        });
        popup.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

截图:

在此输入图像描述

小智 55

您可以测量适配器内容的宽度:

private int measureContentWidth(ListAdapter listAdapter) {
    ViewGroup mMeasureParent = null;
    int maxWidth = 0;
    View itemView = null;
    int itemType = 0;

    final ListAdapter adapter = listAdapter;
    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        final int positionType = adapter.getItemViewType(i);
        if (positionType != itemType) {
            itemType = positionType;
            itemView = null;
        }

        if (mMeasureParent == null) {
            mMeasureParent = new FrameLayout(mContext);
        }

        itemView = adapter.getView(i, itemView, mMeasureParent);
        itemView.measure(widthMeasureSpec, heightMeasureSpec);

        final int itemWidth = itemView.getMeasuredWidth();

        if (itemWidth > maxWidth) {
            maxWidth = itemWidth;
        }
    }

    return maxWidth;
}
Run Code Online (Sandbox Code Playgroud)

并在您的showPopup()函数中:

 ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, STRINGS);
    popup.setAdapter(arrayAdapter);
    popup.setAnchorView(anchorView);
    popup.setContentWidth(measureContentWidth(arrayAdapter));
Run Code Online (Sandbox Code Playgroud)


Kar*_*uri 25

问题在于实施ListPopupWindow.我检查了源代码,并使用setContentWidth(ListPopupWindow.WRAP_CONTENT)setWidth(ListPopupWindow.WRAP_CONTENT)使弹出窗口使用其锚视图的宽度.

  • 嗯,这似乎是一个常见的用例,这是不幸的:( (4认同)

and*_*per 7

我认为最好用的是PopupMenu.例:

final PopupMenu popupMenu = new PopupMenu(mActivity, v);
popupMenu.getMenu().add("test");
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

    @Override
    public boolean onMenuItemClick(final MenuItem item) {
        return true;
    }
});
popupMenu.show();
Run Code Online (Sandbox Code Playgroud)


Jar*_*ard 5

我只想在你的 dimen.xml 文件中设置一个 160dp 左右的尺寸:

<dimen name="overflow_width">160dp</dimen>
Run Code Online (Sandbox Code Playgroud)

然后使用 getDimensionPixelSize 方法设置您的弹出窗口宽度以转换为像素:

int width = mContext.getResources().getDimensionPixelSize(R.dimen.overflow_width); 
mListPopupWindow.setWidth(width);
Run Code Online (Sandbox Code Playgroud)

这应该保持尺寸密度独立。