什么是PopupWindow?
话不多说,直接上图吧!
PopupWindow有什么用?
常用的地方在上面图片里啦,讲这个是因为我想在喜马拉雅实战项目里使用这个玩 意,你觉得怎么样呢?
PopupWindow怎么用?
怎么使用呢?来,看代码!
第一步是创建吧!直接new,那么我们看构造方法:
常用的
无参构造方法
/**
* <p>Create a new empty, non focusable popup window of dimension (0,0).</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*/
public PopupWindow() {
this(null, 0, 0);
}
要显示的内容作为参数的构造方法
/**
* <p>Create a new non focusable popup window which can display the
* <tt>contentView</tt>. The dimension of the window are (0,0).</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*
* @param contentView the popup's content
*/
public PopupWindow(View contentView) {
this(contentView, 0, 0);
}
高度和宽度作为参数的构造方法
/**
* <p>Create a new empty, non focusable popup window. The dimension of the
* window must be passed to this constructor.</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*
* @param width the popup's width
* @param height the popup's height
*/
public PopupWindow(int width, int height) {
this(null, width, height);
}
内容和高度作为参数的构造方法
/**
* <p>Create a new non focusable popup window which can display the
* <tt>contentView</tt>. The dimension of the window must be passed to
* this constructor.</p>
*
* <p>The popup does not provide any background. This should be handled
* by the content view.</p>
*
* @param contentView the popup's content
* @param width the popup's width
* @param height the popup's height
*/
public PopupWindow(View contentView, int width, int height) {
this(contentView, width, height, false);
}
其他的大家去看了。内容、高度、宽度这些都有提供getter和setter方法去获取和设置对应的值。
创建好,我们要设置内容吧!
如果在构造方法里设置了,那么就不用调用setContentView()来设置了!
比如说,我这样写:
package com.sunofbeaches.popwindowdemo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
public class MainActivity extends AppCompatActivity {
private PopupWindow mPopupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建pop
mPopupWindow = new PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//设置backgroud为透明的
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//设置点击pop以外的地方让pop小时,不设置上面这个无效
mPopupWindow.setOutsideTouchable(true);
//载pop的界面
View popView = LayoutInflater.from(this).inflate(R.layout.pop_view, null);
//设置界面
mPopupWindow.setContentView(popView);
}
public void showPop(View view) {
if (!mPopupWindow.isShowing()) {
//显示在这个控件的下方
mPopupWindow.showAsDropDown(view);
}
}
}
布局代码:
就一个按钮而已,添加一个点击事件
弹出的pop布局内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="200dp"
android:gravity="center"
android:background="@drawable/shape_rectangle"
android:layout_height="200dp">
<TextView
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_width="wrap_content"
android:text="弹出的内容..."
android:layout_height="wrap_content" />
</LinearLayout>
看一下运行效果吧:
如果我要展示在上面怎么办呢?
那就得看它展示的方法参数了:
/**
* Display the content view in a popup window anchored to the bottom-left
* corner of the anchor view. If there is not enough room on screen to show
* the popup in its entirety, this method tries to find a parent scroll
* view to scroll. If no parent scroll view can be scrolled, the
* bottom-left corner of the popup is pinned at the top left corner of the
* anchor view.
*
* @param anchor the view on which to pin the popup window
*
* @see #dismiss()
*/
public void showAsDropDown(View anchor) {
showAsDropDown(anchor, 0, 0);
}
展示在anchor的下方,anchor就是参考的view
/**
* Display the content view in a popup window anchored to the bottom-left
* corner of the anchor view offset by the specified x and y coordinates.
* If there is not enough room on screen to show the popup in its entirety,
* this method tries to find a parent scroll view to scroll. If no parent
* scroll view can be scrolled, the bottom-left corner of the popup is
* pinned at the top left corner of the anchor view.
* <p>
* If the view later scrolls to move <code>anchor</code> to a different
* location, the popup will be moved correspondingly.
*
* @param anchor the view on which to pin the popup window
* @param xoff A horizontal offset from the anchor in pixels
* @param yoff A vertical offset from the anchor in pixels
*
* @see #dismiss()
*/
public void showAsDropDown(View anchor, int xoff, int yoff) {
showAsDropDown(anchor, xoff, yoff, DEFAULT_ANCHORED_GRAVITY);
}
第一个参数是参考的view,第二第三个参数是x方向和y方向上的偏移量。
/**
* Displays the content view in a popup window anchored to the corner of
* another view. The window is positioned according to the specified
* gravity and offset by the specified x and y coordinates.
* <p>
* If there is not enough room on screen to show the popup in its entirety,
* this method tries to find a parent scroll view to scroll. If no parent
* view can be scrolled, the specified vertical gravity will be ignored and
* the popup will anchor itself such that it is visible.
* <p>
* If the view later scrolls to move <code>anchor</code> to a different
* location, the popup will be moved correspondingly.
*
* @param anchor the view on which to pin the popup window
* @param xoff A horizontal offset from the anchor in pixels
* @param yoff A vertical offset from the anchor in pixels
* @param gravity Alignment of the popup relative to the anchor
*
* @see #dismiss()
*/
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
最后一个参数是摆放方式,比如说在顶Gravity.TOP
比如说在左边,在右边,在底部…
还有一个这玩意,
/**
* <p>
* Display the content view in a popup window at the specified location. If the popup window
* cannot fit on screen, it will be clipped. See {@link android.view.WindowManager.LayoutParams}
* for more information on how gravity and the x and y parameters are related. Specifying
* a gravity of {@link android.view.Gravity#NO_GRAVITY} is similar to specifying
* <code>Gravity.LEFT | Gravity.TOP</code>.
* </p>
*
* @param parent a parent view to get the {@link android.view.View#getWindowToken()} token from
* @param gravity the gravity which controls the placement of the popup window
* @param x the popup's x location offset
* @param y the popup's y location offset
*/
public void showAtLocation(View parent, int gravity, int x, int y) {
mParentRootView = new WeakReference<>(parent.getRootView());
showAtLocation(parent.getWindowToken(), gravity, x, y);
}
这个方法的参考是全屏幕呢,所以这里的y值基本上不使用。我们实现音乐的列表,使用这种方式即可,比如说:
遇到问题就去看源码吧,这个还是比较简单的!