单击按钮时显示列表视图:Android
问题描述:
我试图在单击按钮时实现下拉列表。单击按钮时显示列表视图:Android
所以,我有一个文本视图和导航栏(nav.xml)中的按钮和相应的列表视图。这个导航栏包含在另一个页面(products.xml)
当按钮被点击时,我得到列表视图正下方的按钮(这是我想要的),但它的我移动当前的所有内容页面向下,即使在导航栏中放置的文本视图也向下移动。
我完全陌生于Android,任何示例或方式如何实现它 ???
答
因此,我们需要使用ListPopupWindow。 链接到官方描述:
http://developer.android.com/reference/android/widget/ListPopupWindow.html
让我们在代码中潜水:
我们有自己的方法:
public void downloadBtnSelected(View anchor) {
final ListPopupWindow lpw = new ListPopupWindow(this);
String[] data = { ".png", ".pdf", ".jpg", ".jpeg" };
PopupAdapter pa = new PopupAdapter(data, this);
lpw.setAdapter(pa);
//setting up an anchor view
lpw.setAnchorView(anchor);
//Setting measure specifications. I'v used this mesure specs to display my
//ListView as wide as my anchor view is
lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
lpw.setWidth(anchor.getRight() - anchor.getLeft());
// Background is needed. You can use your own drawable or make a 9patch.
// I'v used a custom btn drawable. looks nice.
lpw.setBackgroundDrawable(this.getResources().getDrawable(
android.R.drawable.btn_default));
// Offset between anchor view and popupWindow
lpw.setVerticalOffset(3);
lpw.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/// Our action.....
lpw.dismiss();
}
});
lpw.show();
}
并与onClickListener的按钮调用此方法:
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
downloadBtnSelected(v);
}
});
我们传递View v参数作为我们的锚点,以便让我们的PopupWindow知道在哪里显示自己。如果下方有足够的空间,它将显示在我们的锚点视图的左下角。如果没有,它将显示