效果图
核心代码
<span style="font-size:18px;">public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
MyOnclick();
}
/**
* 点击事件
*/
private void MyOnclick() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// PopupWindow的布局文件
View view = getLayoutInflater().inflate(
R.layout.activity_popuwindow, null);
// 设置弹出框的宽高
PopupWindow window = new PopupWindow(view,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置背景
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
// 设置透明度
window.getBackground().setAlpha(100);
// 设置动画,从底部出来
window.setAnimationStyle(android.R.style.Animation_Translucent);
// 点击空白区域消失
window.setOutsideTouchable(true);
// 设置焦点
window.setFocusable(true);
// 可以被触摸
window.setTouchable(true);
// 设置软键盘
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
// 显示的位置,从底部显示
window.showAtLocation(button, Gravity.BOTTOM, 0, 0);
}
});
}
/**
* 获取屏幕尺寸
* 以后可能会用到
*
*/
private void chiCun(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width=displayMetrics.widthPixels;//屏幕宽度
int height=displayMetrics.heightPixels;//屏幕高度
}
}</span>
源码下载
http://download.csdn.net/detail/zhaihaohao1/9472951