Android ListPopupWindow:setWidth和setHeight无效

Android ListPopupWindow:setWidth和setHeight无效,android,view,popupwindow,Android,View,Popupwindow,我正在尝试设置ListPopupWindow的宽度和高度,但运气不好。我读过关于LayatParams的文章,但是ListPopupWindow没有setLayoutParams。如何设置这些参数 ListPopupWindow lpw = new ListPopupWindow(TestActivity.this); CustomAdapter stringAdapter = new CustomAdapter(TestActivity.this, R.layout.row,strings);

我正在尝试设置ListPopupWindow的宽度和高度,但运气不好。我读过关于LayatParams的文章,但是ListPopupWindow没有setLayoutParams。如何设置这些参数

ListPopupWindow lpw = new ListPopupWindow(TestActivity.this);
CustomAdapter stringAdapter = new CustomAdapter(TestActivity.this, R.layout.row,strings);
lpw.setAdapter(stringAdapter);
lpw.setAnchorView(v);
lpw.setWidth(150);
lpw.setHeight(300);
lpw.show();

其中v是一个按钮。

下面的代码对我来说工作得很好。


有两个可能的问题。第一个是适配器,第二个是执行代码的位置。对于后者,请确保您的代码不在onCreate中。

下面的代码对我来说运行得很好。


有两个可能的问题。第一个是适配器,第二个是执行代码的位置。对于后者,请确保您的代码不在onCreate中。

适配器中是否有任何内容?适配器中是否有字符串列表。适配器中是否有任何内容?适配器中是否有字符串列表。适配器中是否可能存在类似问题?适配器中是否可能存在类似问题?
String[] listItems = {"item 1", "item 2 ", "list", "android", "item 3", "foobar", "bar", };
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn=(Button)findViewById(R.id.btnwillappear);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ListPopupWindow lpw = new ListPopupWindow(Test_listpopActivity.this);
            lpw.setAdapter(new ArrayAdapter(Test_listpopActivity.this,  android.R.layout.simple_list_item_1, listItems));
            lpw.setAnchorView(findViewById(R.id.txtwillappear));
            lpw.setWidth(150);
            lpw.setHeight(300);
            lpw.show();

        }
    });

}