下面列出了android.widget.PopupWindow#showAsDropDown ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showPopupWindow() {
View window = getLayoutInflater()
.inflate(R.layout.fragment_home_page_search_popupwindow, null);
window.findViewById(R.id.tv_article).setOnClickListener(this);
window.findViewById(R.id.tv_label).setOnClickListener(this);
window.findViewById(R.id.tv_admin).setOnClickListener(this);
mTypeWindow = new PopupWindow(window,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
true);
mTypeWindow.setOutsideTouchable(true);
mTypeWindow.showAsDropDown(tvType, -tvType.getWidth() / 2, tvType.getHeight() / 2);
}
protected void showPopupWindow() {
Button popupContent=new Button(this);
popupContent.setText(R.string.label_click);
popupContent.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
popupContent.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
final PopupWindow popup=
new PopupWindow(popupContent, popupContent.getMeasuredWidth(),
popupContent.getMeasuredHeight(), true);
popupContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
popup.showAsDropDown(popupAnchor);
}
public void popup(View view){
// 用于PopupWindow的View
View contentView=LayoutInflater.from(this).inflate(R.layout.pp_layout, null, false);
PopupWindow window=new PopupWindow(contentView, 500, 500, true);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setOutsideTouchable(true);
window.setTouchable(true);
final DoubleHeadedDragonBar bar3 = contentView.findViewById(R.id.bar3);
bar3.setUnit("0", "100");
bar3.setMinValue(10);
bar3.setMaxValue(80);
TextView testView2 = (TextView) LayoutInflater.from(this).inflate(R.layout.toast_view, null);
bar3.setToastView(testView2);
TextView testView1 = (TextView) LayoutInflater.from(this).inflate(R.layout.toast_view, null);
bar3.setToastView1(testView1);
window.showAsDropDown(view);
window.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
bar3.close();
}
});
}
public void doClick(View view) {
//AlertDialog����
// 1.����AlertDialog
// AlertDialog dialog = new AlertDialog.Builder(this).setTitle("title")
// .setMessage("message").create();
// 2.ͨ��Dialog��ȡWindow
// Window window = dialog.getWindow();
// window.setGravity(Gravity.BOTTOM); // �˴���������dialog��ʾ��λ��
// 3.��Window�������ö������
// window.setWindowAnimations(R.style.dialogWindowAnim); // ��Ӷ���
// ��ʾ�Ի���
// dialog.show();
//popupwindow�Ķ���
View popupwindow = getLayoutInflater().inflate(
R.layout.popupwindow_layout, null);
PopupWindow pop = new PopupWindow(popupwindow,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setAnimationStyle(R.style.dialogWindowAnim);
pop.showAsDropDown(view);
}
@UiThread public static void revealPopupWindow(@NonNull PopupWindow popupWindow, @NonNull View from) {
Rect rect = ViewHelper.getLayoutPosition(from);
int x = (int) rect.exactCenterX();
int y = (int) rect.exactCenterY();
if (popupWindow.getContentView() != null) {
View view = popupWindow.getContentView();
if (view != null) {
popupWindow.showAsDropDown(from);
view.post(() -> {
if (ViewCompat.isAttachedToWindow(view)) {
Animator animator = ViewAnimationUtils.createCircularReveal(view, x, y, 0,
(float) Math.hypot(rect.width(), rect.height()));
animator.setDuration(view.getResources().getInteger(android.R.integer.config_shortAnimTime));
animator.start();
}
});
}
}
}
/**
* 得到一个自动识别在目标控件上方或下方的pupupwindow并显示
*
* @param contentView popupwindow要显示的视图
* @param width popupwindow的宽度
* @param activity 能得到getWindowManager()的上下文
* @return
*/
public static PopupWindow getPopupWindowAsDropDownParentAuto(View contentView, int width, int height, View anchorView, Activity activity) {
// View itemView = (View) contentView.getParent();// 得到contentView的父控件
PopupWindow popupWindow = getPopupWindow(contentView, width, height);
// 控制它放置的位置
if (isShowBottom(activity, anchorView)) {// 显示popupwindow在itemView的下方,偏移量都为0
popupWindow.showAsDropDown(anchorView, 0, 0);
} else {// 显示popupwindow在itemView的上方,偏移量y都为-2*itemView.getHeight()
popupWindow.showAsDropDown(anchorView, 0,
-2 * anchorView.getHeight());
}
return popupWindow;
}
public static void showPopup(View v) {
final Context c = v.getContext();
LayoutInflater inflater = LayoutInflater.from(c);
TextView contentView = (TextView) inflater.inflate(R.layout.participant_popup_item, null);
contentView.setText(v.getTag(R.id.poster_name).toString());
PopupWindow popup = new PopupWindow(c);
popup.setContentView(contentView);
popup.setOutsideTouchable(true);
popup.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.poster_popup_bg));
popup.showAsDropDown(v);
}
private void showPopup(View view, String walletAddress) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View popupView = inflater.inflate(R.layout.popup_remind_later, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, true);
popupView.setOnClickListener(v -> {
viewModel.setIsDismissed(walletAddress, true).subscribe(this::backedUp);
popupWindow.dismiss();
});
popupWindow.showAsDropDown(view, 0, 0);
}
private void showPopup(View view, WarningData data) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View popupView = inflater.inflate(R.layout.popup_remind_later, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, true);
popupView.setOnClickListener(v -> {
data.callback.remindMeLater(data.wallet);
popupWindow.dismiss();
});
popupWindow.showAsDropDown(view, 0, 20);
}
private void showPopupWindow() {
//创建对象
PopupWindow popupWindow = new PopupWindow(this);
View inflate = LayoutInflater.from(this).inflate(R.layout.view_pop_custom, null);
//设置view布局
popupWindow.setContentView(inflate);
popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
//设置动画的方法
popupWindow.setAnimationStyle(R.style.BottomDialog);
//设置PopUpWindow的焦点,设置为true之后,PopupWindow内容区域,才可以响应点击事件
popupWindow.setTouchable(true);
//设置背景透明
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
//点击空白处的时候让PopupWindow消失
popupWindow.setOutsideTouchable(true);
// true时,点击返回键先消失 PopupWindow
// 但是设置为true时setOutsideTouchable,setTouchable方法就失效了(点击外部不消失,内容区域也不响应事件)
// false时PopupWindow不处理返回键,默认是false
popupWindow.setFocusable(false);
//设置dismiss事件
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
}
});
boolean showing = popupWindow.isShowing();
if (!showing){
//show,并且可以设置位置
popupWindow.showAsDropDown(mTv1);
}
//popupWindow.dismiss();
}
private void displayStatsPopup(View anchor) {
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_window_flow_stats, null);
RelativeLayout viewGroup = (RelativeLayout) layout.findViewById(R.id.popup_stats);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(layout, RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView elementCount = (TextView) viewGroup.findViewById(R.id.stats_total_elements);
TextView timeEstimate = (TextView) viewGroup.findViewById(R.id.stats_total_time);
TextView completeCount = (TextView) viewGroup.findViewById(R.id.stats_times_complete);
TextView lifeTimeInFlow = (TextView) viewGroup.findViewById(R.id.stats_life_time_spent_in_flow);
elementCount.setText(
String.valueOf(currentToDay.getChildCount())
);
timeEstimate.setText(
String.valueOf(currentToDay.getFormattedTime())
);
completeCount.setText(
String.valueOf(currentToDay.getCompletionTokens())
);
lifeTimeInFlow.setText(
String.valueOf(
AppUtils.buildTimerStyleTime(currentToDay.getLifeTimeInToDay()))
);
popup.setBackgroundDrawable(new BitmapDrawable(null,""));
popup.setFocusable(true);
popup.showAsDropDown(anchor);
}
private void materialSelected(View sender, TradeMaterialState materialState) {
View popupView = activity.getLayoutInflater().inflate(R.layout.popup_trade_material, null);
ImageView infiniteButton = popupView.findViewById(R.id.imageView_tradeButton_infinite);
ImageView noneButton = popupView.findViewById(R.id.imageView_tradeButton_none);
ImageView eightMoreButton = popupView.findViewById(R.id.imageView_tradeButton_eightMore);
ImageView oneMoreButton = popupView.findViewById(R.id.imageView_tradeButton_oneMore);
ImageView oneLessButton = popupView.findViewById(R.id.imageView_tradeButton_oneLess);
ImageView eightLessButton = popupView.findViewById(R.id.imageView_tradeButton_eightLess);
OriginalImageProvider.get(imageInfinite).setAsImage(infiniteButton);
OriginalImageProvider.get(imageNone).setAsImage(noneButton);
OriginalImageProvider.get(imageEightMore).setAsImage(eightMoreButton);
OriginalImageProvider.get(imageOneMore).setAsImage(oneMoreButton);
OriginalImageProvider.get(imageOneLess).setAsImage(oneLessButton);
OriginalImageProvider.get(imageEightLess).setAsImage(eightLessButton);
infiniteButton.setOnClickListener(v -> changeTradeMaterialAmount(materialState.getMaterialType(), Integer.MAX_VALUE, false));
noneButton.setOnClickListener(v -> changeTradeMaterialAmount(materialState.getMaterialType(), 0, false));
eightMoreButton.setOnClickListener(v -> changeTradeMaterialAmount(materialState.getMaterialType(), TRADING_MULTIPLE_STEP_INCREASE, true));
oneMoreButton.setOnClickListener(v -> changeTradeMaterialAmount(materialState.getMaterialType(), 1, true));
oneLessButton.setOnClickListener(v -> changeTradeMaterialAmount(materialState.getMaterialType(), -1, true));
eightLessButton.setOnClickListener(v -> changeTradeMaterialAmount(materialState.getMaterialType(), -TRADING_MULTIPLE_STEP_INCREASE, true));
popupView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int xOffset = -((popupView.getMeasuredWidth() - sender.getWidth()) / 2);
int yOffset = -(popupView.getMeasuredHeight() + sender.getHeight());
popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(sender, xOffset, yOffset);
}
public static void showPopup(View v) {
final Context c = v.getContext();
LayoutInflater inflater = LayoutInflater.from(c);
TextView contentView = (TextView) inflater.inflate(R.layout.participant_popup_item, null);
contentView.setText(v.getContentDescription());
PopupWindow popup = new PopupWindow(c);
popup.setContentView(contentView);
popup.setOutsideTouchable(true);
popup.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.poster_popup_bg));
popup.showAsDropDown(v, 0, -v.getHeight() * 2);
}
/**
* 展示popupWindow--解决Android 7.0版本兼容性问题
*
* @param mWindow PopupWindow
* @param anchor the view on which to pin the popup window
* @param x A horizontal offset from the anchor in pixels
* @param y A vertical offset from the anchor in pixels
*/
public static void showAsDropDown(PopupWindow mWindow, View anchor, int x, int y) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
int[] location = new int[2];
anchor.getLocationOnScreen(location);
mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, location[0] + x, location[1] + anchor.getHeight() + y);
} else {
mWindow.showAsDropDown(anchor, x, y);
}
}
private void showPopupWindow() {
View contentView = LayoutInflater.from(PopupShowAsDropDownActivity.this).inflate(R.layout.show_as_drop_down_activity_popup, null);
mPopWindow = new PopupWindow(contentView);
mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tv1 = contentView.findViewById(R.id.pop_computer);
TextView tv2 = contentView.findViewById(R.id.pop_financial);
TextView tv3 = contentView.findViewById(R.id.pop_manage);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
mPopWindow.showAsDropDown(mMenuTv);
}
@Override
public void onMenuClick() {
PopupWindow popupWindow = HomeMenuWindow.getHomeMenu(this.getActivity());
popupWindow.showAsDropDown(mMenuLayout, 0, ScreenUtils.dimen2px(this, R.dimen.s_20));
}
public void showAsDropDown(PopupWindow popup, View anchor, int xoff, int yoff, int gravity) {
popup.showAsDropDown(anchor, xoff, yoff);
}
@Override
public void showAsDropDown(PopupWindow popup, View anchor, int xoff, int yoff,
int gravity) {
popup.showAsDropDown(anchor, xoff, yoff);
}
public static void showAsDropDown(PopupWindow popup, View anchor, int xoff, int yoff,
int gravity) {
popup.showAsDropDown(anchor, xoff, yoff, gravity);
}
/**
* 得到在指定某个视图外的popupwindow并显示
*
* @param contentView popupwindow要显示的视图
* @param width popupwindow的宽度
* @param height popupwindow的高度
* @param anchorView 参考视图
* @param xoff x轴偏移量
* @param yoff y轴偏移量
* @return
*/
public static PopupWindow getPopupWindowAsDropDown(View contentView, int width, int height, View anchorView, int xoff, int yoff) {
PopupWindow popupWindow = getPopupWindow(contentView, width, height);
popupWindow.showAsDropDown(anchorView, xoff, yoff);
return popupWindow;
}