例如淘寶摄职,京東之類的點擊購買之后朗鸠,就會出現(xiàn)一個PopupWindow的窗口從下面彈出來,以便顧客更好的體驗和方便顧客的操作性莫矗。
![(P%EK9J)U[9%(G$ES6`T}W.png
顯示popupWindow
private void showPopwindow() {
// 1 利用layoutInflater獲得View
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popupwindow_add_product, null);
// 2下面是兩種方法得到寬度和高度 getWindow().getDecorView().getWidth()
final PopupWindow window = new PopupWindow(view,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
// 3 參數(shù)設(shè)置// 設(shè)置popWindow彈出窗體可點擊,這句話必須添加,并且是true
window.setFocusable(true);
// 實例化一個ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xFFFFFFFF);
window.setBackgroundDrawable(dw);
// 設(shè)置popWindow的顯示和消失動畫
window.setAnimationStyle(R.style.mypopwindow_anim_style);
// 4 控件處理
ImageView iv_goodinfo_photo = (ImageView) view.findViewById(R.id.iv_goodinfo_photo);
TextView tv_goodinfo_name = (TextView) view.findViewById(R.id.tv_goodinfo_name);
TextView tv_goodinfo_price = (TextView) view.findViewById(R.id.tv_goodinfo_price);
NumberAddSubView nas_goodinfo_num = (NumberAddSubView) view.findViewById(R.id.nas_goodinfo_num);
Button bt_goodinfo_cancel = (Button) view.findViewById(R.id.bt_goodinfo_cancel);
Button bt_goodinfo_confim = (Button) view.findViewById(R.id.bt_goodinfo_confim);
// 加載圖片
Glide.with(GoodsInfoActivity.this).load(Constants.Base_URl_IMAGE + goods_bean.getFigure()).into(iv_goodinfo_photo);
// 名稱
tv_goodinfo_name.setText(goods_bean.getName());
// 顯示價格
tv_goodinfo_price.setText(goods_bean.getCover_price());
// 設(shè)置最大值和當(dāng)前值
nas_goodinfo_num.setMaxValue(5);
nas_goodinfo_num.setValue(goods_bean.getNumber());
//這是自定義view中接口的回調(diào)方法作谚,以便能操作view視圖
nas_goodinfo_num.setOnNumberChangeListener(new NumberAddSubView.OnNumberChangeListener() {
@Override public void addNumber(View view, int value) {
int number = goods_bean.getNumber();
goods_bean.setNumber(number + 1);
if (number + 1 >= 5) {
Toast.makeText(GoodsInfoActivity.this, "該商品最多購買5個",Toast.LENGTH_SHORT).show();
goods_bean.setNumber(5);
}
}
@Override public void subNumner(View view, int value) {
int number = goods_bean.getNumber();
goods_bean.setNumber(number - 1);
if (number <= 1) {
Toast.makeText(GoodsInfoActivity.this, "該商品最少購買1個", Toast.LENGTH_SHORT).show();
goods_bean.setNumber(1);
}
}
});
// 5 在底部顯示
window.showAtLocation(GoodsInfoActivity.this.findViewById(R.id.ll_goods_root),
Gravity.BOTTOM, 0, VirtualkeyboardHeight.getBottomStatusHeight(GoodsInfoActivity.this));