Android控件之PopupWindow弹出窗口:
package com.example.popupwindow;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button1(View v) {
View view=getLayoutInflater().inflate(R.layout.popup,null);
//实例化创建PopupWindow()
//参数(窗体的视图,宽,高)
PopupWindow popup=new PopupWindow(view,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//背景颜色
//设置弹出窗口的动画样式,这个动画样式是从旁边飞出来的
popup.setAnimationStyle(android.R.style.Animation_Translucent);
popup.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.alert_dark_frame));
//设置背景透明色
popup.getBackground().setAlpha(100);
//设置点击边上可以消失
popup.setOutsideTouchable(true);
//设置是否可以获取焦点
popup.setFocusable(true);
//设置是否可以触摸
popup.setTouchable(true);
//设置软件盘
popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//开始显示show;
popup.showAtLocation(v,Gravity.BOTTOM,0,0);
//插播一段知识点(如何获取设备的尺寸)
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
}
}