标签:
/**自定义Popup:START**/
private PopupWindow mPopupWindow;
private void initPopup() {
/*生成popup的视图*/
View popupWindow = createPopupView();
/*设置popup的宽度和高度*/
int width = getResources().getDisplayMetrics().widthPixels;//得到当前显示设备的宽度,单位是像素
int height = getResources().getDisplayMetrics().heightPixels;//得到当前显示设备的宽度,单位是像素
if(dataSize>5){
mPopupWindow = new PopupWindow(popupWindow, 540 * width / 645,1 * height / 2, true);
}else {
mPopupWindow = new PopupWindow(popupWindow, 540 * width / 645,WindowManager.LayoutParams.WRAP_CONTENT, true);
}
/*配置pop*/
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);//设置是否点击PopupWindow外退出PopupWindow
mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));//设置背景为空Drawable对象,如不设置,则无法退出
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
/*popup的隐藏监听,恢复主界面亮度*/
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.alpha = 1.0f;
getWindow().setAttributes(params);
}
});
}
private void showPopup() {
/*背景变暗*/
WindowManager.LayoutParams params = getWindow().getAttributes();//创建当前界面的一个参数对象
params.alpha = 0.7f;
getWindow().setAttributes(params);
/*显示popup*/
mPopupWindow.showAtLocation(mainView, Gravity.CENTER, 0, 0); //参数:父View对象,重心,x轴偏移量,y轴的偏移量
}
private View createPopupView() {
/*加载视图*/
View popupWindow = LayoutInflater.from(this).inflate(R.layout.popup_borrowreason, null);
ListView listview = (ListView) popupWindow.findViewById(R.id.listview);
/*绑定数据*/
ArrayList<String> list = getBorrowReason();
ArrayAdapter<String> adapter;
adapter= new ArrayAdapter(this,R.layout.item_borrowreason,R.id.textView,list);//使用自定义布局
listview.setAdapter(adapter);
/*设置监听*/
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AbToastUtil.showToast(context, "你点击了:popup=" + position);
}
});
return popupWindow;
}
int dataSize;
private ArrayList<String> getBorrowReason() {
ArrayList<String> list = new ArrayList<String>();
list.add("没钱买吃的");
list.add("没钱买喝的");
list.add("没钱买玩的");
list.add("没钱买乐的");
dataSize=list.size();
return list;
}
/**自定义Popup:END**/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:background="@drawable/shape_white_bg04"
android:orientation="vertical">
<LinearLayout
android:background="@drawable/shape_white_bg03"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="借款原因"
android:textColor="#0099FF"
android:textSize="20dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cdcdcd" />
<ListView
android:listSelector="#ffffff"
android:scrollbars="none"
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"></ListView>
<View
android:layout_width="match_parent"
android:layout_height="5dp"/>
</LinearLayout>
</LinearLayout>
标签:
原文地址:http://www.cnblogs.com/yutianran/p/4739396.html