Activity中
private Button mBt;
private View mView;
private Button btnSaveProject,btnAbandonProject,btnCancelProject;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mBt = (Button) findViewById(R.id.bt);
mBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater= (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.finish_project_popuwindow, null);
btnSaveProject = (Button) mView.findViewById(R.id.popupwindow_Button_saveProject);
btnAbandonProject = (Button) mView.findViewById(R.id.popupwindow_Button_abandonProject);
btnCancelProject = (Button) mView.findViewById(R.id.popupwindow_cancelButton);
popupWindow = new PopupWindow(mView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置按钮监听
btnCancelProject.setOnClickListener(new View.OnClickListener() {
@SuppressLint("LongLogTag")
@Override
public void onClick(View v) {
Log.i("xue", "取消项目");
popupWindow.dismiss();
}
});
//设置PopupWindow的View
popupWindow.setContentView(mView);
//设置PopupWindow弹出窗体的宽
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//设置PopupWindow弹出窗体的高
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//设置PopupWindow弹出窗体可点击
popupWindow.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
popupWindow.setAnimationStyle(R.style.Animation);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw= new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
popupWindow.setBackgroundDrawable(dw);
popupWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
}
});
}
新建anim文件夹
enter_anim.xml中
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
>
<translate
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="100" />
<alpha
android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="100"/>
</set>
out_anim.xml中
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
>
<translate
android:fromYDelta="0"
android:toYDelta="100%p"
android:duration="100"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0"
android:duration="100"/>
</set>
style中
<style name="Animation">
<item name="android:windowEnterAnimation">@anim/enter_anim</item>
<item name="android:windowExitAnimation">@anim/out_anim</item>
</style>
网友评论