我正试图设置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是按钮。
答案 0 :(得分:4)
以下代码对我来说非常好。
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();
}
});
}
有两个可能的问题。首先是你的适配器,第二个是你的代码执行的地方。对于后者,请确保您的代码不在onCreate中。