我坚持使用弹出窗口定位。我点击按钮时显示弹出窗口。我希望它应根据可用空间定位。此外,如果我的按钮位于中心,它应该在按钮下方。下面是我的代码。请让我知道我哪里错了。谢谢。
mBtnPopUp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
ListView mListView = (ListView) mPopUpView.findViewById(R.id.pop_up_list_view);
mListView.setAdapter(mPopupListAdapter);
Drawable drawable = getResources().getDrawable(android.R.drawable.alert_light_frame);
mPopupWindow.setBackgroundDrawable(drawable);
// mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
showLikeQuickAction(0, 0);
mPopupWindow.showAsDropDown(mBtnPopUp);
}
});
mPopupWindow.setOutsideTouchable(true);
public void showLikeQuickAction(int xOffset, int yOffset)
{
int[] location = new int[2];
mBtnPopUp.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0] + mBtnPopUp.getWidth(), location[1] + mBtnPopUp.getHeight());
mBtnPopUp.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = mBtnPopUp.getMeasuredWidth();
int rootHeight = mBtnPopUp.getMeasuredHeight();
@SuppressWarnings("deprecation")
int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
int xPos = screenWidth - rootWidth + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;
if(rootWidth > anchorRect.right - anchorRect.left)
{
// right
xPos = anchorRect.right - rootWidth;
}
else
{
// left
xPos = anchorRect.left + 15;
}
if(xPos + rootWidth > screenWidth)
xPos = screenWidth - rootWidth - 20;
// display on bottom
if(rootHeight > anchorRect.top)
{
yPos = anchorRect.bottom + yOffset;
// mPopupWindow.setAnimationStyle(R.style.Animations_GrowFromTop);
}
mPopupWindow.showAtLocation(mBtnPopUp, Gravity.NO_GRAVITY, xPos, yPos);
}
谢谢..
答案 0 :(得分:2)
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<?> parent, View view, int position, long id) {
// Our action.....
lpw.dismiss();
}
});
lpw.show();
}
和带有onClickListener的按钮来调用此方法:
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
downloadBtnSelected(v);
}
});
这段代码让你在btn下面显示一个弹出窗口 弹出窗口宽度与按钮1相同。 如果你想填充屏幕宽度 - 设置宽度= LinearLayout.MATCH_PARENT 如果是空格,弹出菜单将显示在锚点视图下方,如果下面没有空格,则弹出菜单将显示在
之外答案 1 :(得分:1)
我知道它很安静,但我希望它会帮助别人。
LayoutInflater layoutInflater = getLayoutInflater();
int popupWidth = 500;//ViewGroup.LayoutParams.WRAP_CONTENT;
int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
popupView = layoutInflater.inflate(R.layout.main, null);
PopupWindow attachmentPopup = new PopupWindow(this);
attachmentPopup.setFocusable(true);
attachmentPopup.setWidth(popupWidth);
attachmentPopup.setHeight(popupHeight);
attachmentPopup.setContentView(popupView);
attachmentPopup.setBackgroundDrawable(new BitmapDrawable());
attachmentPopup.showAsDropDown(v, -5, 0);
它将根据按钮的位置定位弹出窗口。
答案 2 :(得分:0)
对于自定义弹出窗口,请创建一个名为
的XML文件cust_toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="45dp" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="25sp"
android:textStyle="bold"
android:gravity="center"
android:paddingTop="200dp"/>
</LinearLayout>
然后在java文件中你想要这个弹出窗口添加下面的代码
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.cust_toast_layout,
(ViewGroup)findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
//text.setText("Excellent !!!");
text.setBackgroundResource(R.drawable.right_ans);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
在按钮上,您可以调用一个方法,并在该方法中包含此代码。 您可以通过在xml文件中编辑来根据您的要求进行更改。
答案 3 :(得分:0)
此代码将在您特定视图的中心显示完美的弹出窗口
// click event of specific view
@Override
public void onClick(View v) {
super.onClick(v);
if (v == lin_filterActionbar) {
PopupWindow popupwindow_obj; // create object
popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
popupwindow_obj.showAsDropDown(lin_filterActionbar);
}
}
private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {
final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);
View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
dialog_AllergiesSelect.setContentView(v);
dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setFocusable(true);
// TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
// LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
// lis_testSelection.setLayoutParams(lin_laLayoutParams);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
//dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);
Rect location = locateView(lin);
dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);
// Getting a reference to Close button, and close the popup when clicked.
return dialog_AllergiesSelect;
}
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}