popwindow使用
最终实现的效果:
1.popwindow 布局文件:popwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="325dp"
android:layout_gravity="bottom"
android:layout_height="150dp">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#16c233"
android:gravity="center_vertical"
android:layout_height="30dp">
<Button
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#16c233"
android:textSize="15sp"
android:textColor="#ffffff"
android:text="@string/book_ensure"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#16c233"
android:layout_marginLeft="80dp"
android:textSize="15dp"
android:textColor="#ffffff"
android:text="@string/cancel"
/>
</LinearLayout>
<ListView
android:id="@+id/lv_workNum"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
2.popwindow 设置
LayoutInflater mLayoutInflater = LayoutInflater.from(this);
contentView = mLayoutInflater.inflate(R.layout.popupwindow, null);
pop = new PopupWindow(contentView, 750, 450);//布局、宽高
Button ensure = (Button) contentView.findViewById(R.id.btn_confirm);
Button cancel = (Button) contentView.findViewById(R.id.btn_cancel);
ensure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(IsCheckedWorkerNum){
......
pop.dismiss();
}else{
......
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pop.dismiss();
}
});
ListView listView = (ListView) contentView.findViewById(R.id.lv_workNum);
// 产生背景变暗效果
WindowManager.LayoutParams lp = activity. getWindow().getAttributes();
lp.alpha = 0.4f;
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
activity.getWindow().setAttributes(lp);
pop.setTouchable(true);
pop.setFocusable(true);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);//点击外部,popwindow消失
pop.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
pop.update();
pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
// 在dismiss中恢复透明度
public void onDismiss() {
popDismiss();
}
});
workerNumAdapter=new WorkerNumAdapter(this,works);
listView.setAdapter(workerNumAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
workerNumAdapter.setSeclection(position);
workerNumAdapter.notifyDataSetChanged();
workerNum=works[position];
IsCheckedWorkerNum=true;
}
});
3.恢复透明度
private void popDismiss(){
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = 1f;
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
activity.getWindow().setAttributes(lp);
}
相关推荐:
1.http://www.cnblogs.com/sw926/p/3230659.html
2.http://www.cnblogs.com/mengdd/p/3569127.html
3.https://my.oschina.net/lhjtianji/blog/54530
4.简单demo:http://download.csdn.net/detail/huangbiao86/3966917