private PopupWindow popupWindow; private GridView gv_invite; private void initPopWindow() { LayoutInflater inflater = LayoutInflater .from(ProjectDetailActivity.this); View contentView = inflater.inflate(R.layout.popupwindow_enjoy, null); gv_invite = (GridView) contentView.findViewById(R.id.gv_invite); gv_invite.setAdapter(new AdapterEnjoy(ProjectDetailActivity.this)); gv_invite.setOnItemClickListener(itemListener); TextView tv_cancel = (TextView) contentView .findViewById(R.id.tv_cancel); TextView tv_title = (TextView) contentView.findViewById(R.id.tv_title); tv_title.setText("分享好友"); tv_cancel.setOnClickListener(listener1); // 获取屏幕的宽度和高度 根据宽高设置 popupwindow宽高 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); // int width = lay_redpacket.getWidth(); // 父控件的宽度 // int heigth = dm.heightPixels / 2; popupWindow = new PopupWindow(contentView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new PaintDrawable()); popupWindow.setAnimationStyle(R.style.popupWindowAnim); popupWindow.update(); popupWindow.setFocusable(true); // popupWindow.showAtLocation(pp, Gravity.BOTTOM, 0, 0); ColorDrawable dw = new ColorDrawable(100); popupWindow.setBackgroundDrawable(dw); // listViewPop.getLayoutParams().height = heigth; // listViewPop.getLayoutParams().width = width; contentView.setOnClickListener(listener1); popupWindow.setFocusable(false); popupWindow.setOutsideTouchable(true); // 允许在外点击隐藏 popupwindow popupWindow.setBackgroundDrawable(new BitmapDrawable()); }
<pre name="code" class="html"> import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class AdapterEnjoy extends BaseAdapter { String[] enjoyName = { "微信", "微信朋友圈", "手机QQ", "信息", "新浪微博" }; int[] enjoyImg = { R.drawable.enjoy_weixin, R.drawable.enjoy_weixin_friends, R.drawable.enjoy_qq, R.drawable.enjoy_message, R.drawable.enjoy_sina }; Context context = null; LayoutInflater inflater = null; public AdapterEnjoy(Context context) { super(); this.context = context; inflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return 5; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { convertView = inflater.inflate(R.layout.item_gv_invite, null); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.icon.setImageResource(enjoyImg[position]); holder.name.setText(enjoyName[position]); return convertView; } class ViewHolder { TextView name; ImageView icon; public ViewHolder(View v) { super(); name = (TextView) v.findViewById(R.id.tv_enjoyName); icon = (ImageView) v.findViewById(R.id.iv_enjoyIcon); } } }
<?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:background="#30000000" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/shape_dialog" android:orientation="vertical" android:padding="5dp" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:text="分享给你的好友,一起拼手气刮奖" android:textColor="@color/text_hint" /> <GridView android:id="@+id/gv_invite" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@color/white" android:cacheColorHint="#00000000" android:listSelector="@color/transparent" android:numColumns="3" android:scrollbars="none" > </GridView> </LinearLayout> <TextView android:id="@+id/tv_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:padding="10dp" android:background="@drawable/shape_dialog" android:gravity="center" android:text="改天吧" android:textColor="@color/red" /> </LinearLayout>
<?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:gravity="center" android:padding="10dp" android:orientation="vertical" > <ImageView android:id="@+id/iv_enjoyIcon" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/enjoy_weixin" /> <TextView android:id="@+id/tv_enjoyName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="14sp" android:text="微信" /> </LinearLayout>