popup_window.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#ddffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="haha"
android:id="@+id/bt_haha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="bottom"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
public void showMenu() {
Log.e("TAG","showMenu");
View popView = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popup_windiow, mRootView, false);
/*
Rect rect = new Rect();
Point offset = new Point();
mRootView.getGlobalVisibleRect(rect,offset);
rect.offset(-offset.x,-offset.y);
*/
final PopupWindow pop = new PopupWindow(popView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT,true);
pop.setBackgroundDrawable(new BitmapDrawable());
popView.findViewById(R.id.bt_haha).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pop.dismiss();
}
});
pop.showAtLocation(mRootView, Gravity.CENTER,0,getStatusBarHeight());
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
现在出现的问题是,popwindow会超出屏幕边距
public class Screen {
private int width;
private int height;
public Screen(Activity context) {
DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics );
width = metrics.widthPixels;
height = metrics.heightPixels;
}
/**
* @return 屏幕宽度 in pixel
*/
public int getWidth() {
return width;
}
/**
*
* @return 屏幕高度 in pixel
*/
public int getHeight() {
return height;
}
@Override
public String toString() {
return "Screen{" +
"width=" + width +
", height=" + height +
'}';
}
}
public void showMenu() {
Log.e("TAG","showMenu");
View popView = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popup_windiow, mRootView, false);
/*
Rect rect = new Rect();
Point offset = new Point();
mRootView.getGlobalVisibleRect(rect,offset);
rect.offset(-offset.x,-offset.y);
*/
final PopupWindow pop = new PopupWindow(popView, mScreen.getWidth(), mScreen.getHeight()-getStatusBarHeight(),true);
pop.setBackgroundDrawable(new BitmapDrawable());
popView.findViewById(R.id.bt_haha).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pop.dismiss();
}
});
pop.showAtLocation(mRootView, Gravity.NO_GRAVITY,0,getStatusBarHeight());
}