作者:aaa | 来源:互联网 | 2023-01-30 14:44
当用户单击三个蓝点按钮时,我想要这样的弹出窗口。如何在android中实现这种类型的弹出视图。有图书馆可用吗?
1> Nilesh Ratho..:
您可以使用该库来实现您的目标
Android工具提示库1
Android工具提示库2
Android工具提示库3
Android工具提示库4
如果您要创建自定义类别,而不是使用以下类,则创建一个这样的自定义类别
public class TooltipWindow {
private static final int MSG_DISMISS_TOOLTIP = 100;
private Context ctx;
private PopupWindow tipWindow;
private View contentView;
private LayoutInflater inflater;
public TooltipWindow(Context ctx) {
this.ctx = ctx;
tipWindow = new PopupWindow(ctx);
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cOntentView= inflater.inflate(R.layout.tooltip_layout, null);
}
void showToolTip(View anchor) {
tipWindow.setHeight(LayoutParams.WRAP_CONTENT);
tipWindow.setWidth(LayoutParams.WRAP_CONTENT);
tipWindow.setOutsideTouchable(true);
tipWindow.setTouchable(true);
tipWindow.setFocusable(true);
tipWindow.setBackgroundDrawable(new BitmapDrawable());
tipWindow.setContentView(contentView);
int screen_pos[] = new int[2];
// Get location of anchor view on screen
anchor.getLocationOnScreen(screen_pos);
// Get rect for anchor view
Rect anchor_rect = new Rect(screen_pos[0], screen_pos[1], screen_pos[0]
+ anchor.getWidth(), screen_pos[1] + anchor.getHeight());
// Call view measure to calculate how big your view should be.
contentView.measure(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
int cOntentViewHeight= contentView.getMeasuredHeight();
int cOntentViewWidth= contentView.getMeasuredWidth();
// In this case , i dont need much calculation for x and y position of
// tooltip
// For cases if anchor is near screen border, you need to take care of
// direction as well
// to show left, right, above or below of anchor view
int position_x = anchor_rect.centerX() - (contentViewWidth / 2);
int position_y = anchor_rect.bottom - (anchor_rect.height() / 2);
tipWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, position_x, position_y);
// send message to handler to dismiss tipWindow after X milliseconds
handler.sendEmptyMessageDelayed(MSG_DISMISS_TOOLTIP, 4000);
}
boolean isTooltipShown() {
if (tipWindow != null && tipWindow.isShowing())
return true;
return false;
}
void dismissTooltip() {
if (tipWindow != null && tipWindow.isShowing())
tipWindow.dismiss();
}
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_DISMISS_TOOLTIP:
if (tipWindow != null && tipWindow.isShowing())
tipWindow.dismiss();
break;
}
}
;
};
}
现在,为您的自定义布局(例如tooltip_layout)
像这样创建一个可绘制文件nav_up
-
现在像这样使用此工具提示
TooltipWindow tipWindow = new TooltipWindow(MainActivity.this);
@Override
public void onClick(View v) {
if (!tipWindow.isTooltipShown())
tipWindow.showToolTip(v);
}
如有疑问请问我