Android PopupWindow做的分享界面

package com.tq.mbaexam.view;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;


import com.tq.mbaexam.R;
import com.tq.mbaexam.activity.NLPGActivity;


public class CustomPopupWindowView extends PopupWindow {

private View view;
private GridView gridView;
private TextView cancel;
private int[] logo = {R.drawable.logo_sinaweibo, R.drawable.logo_tencentweibo
, R.drawable.logo_kaixin, R.drawable.logo_qzone, R.drawable.logo_renren};
private String[] name = {"新浪微博", "腾讯微博", "开心网", "QQ空间", "人人网"};
private List<Map<String, Object>> contents;

public CustomPopupWindowView(NLPGActivity context,OnItemClickListener listener){
LayoutInflater inflater = (LayoutInflater) context  
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
contents  = new ArrayList<Map<String,Object>>();
        view = inflater.inflate(R.layout.popup_window_view, null);  
        gridView = (GridView) view.findViewById(R.id.share_gridview);
        cancel = (TextView) view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
        this.setContentView(view);
        //设置SelectPicPopupWindow弹出窗口的宽  
        this.setWidth(LayoutParams.MATCH_PARENT);  
        //设置SelectPicPopupWindow弹出窗口的高  
        this.setHeight(LayoutParams.WRAP_CONTENT);  
        //设置SelectPicPopupWindow弹出窗口可点击  
        this.setFocusable(true);  
        //设置SelectPicPopupWindow弹出窗口动画效果  
        this.setAnimationStyle(R.style.mystyle);  
        //实例化一个ColorDrawable颜色为半透明  
        ColorDrawable dw = new ColorDrawable(-00000);  
        //设置SelectPicPopupWindow弹出窗口的背景  
        this.setBackgroundDrawable(dw);  
        //mMenuView加入OnTouchListener监听推断获取触屏位置假设在选择框外面则销毁弹出框  
        view.setOnTouchListener(new OnTouchListener() {  
               
            public boolean onTouch(View v, MotionEvent event) {  
                   
                int height = view.findViewById(R.id.popup_window_view_layout).getTop();  
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){  
                    if(y<height){  
                        dismiss();  
                    }  
                }                 
                return true;  
            }  
        });  
         
        for (int i = 0; i < name.length; i++) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("logo", logo[i]);
map.put("name", name[i]);
contents.add(map);
}
        SimpleAdapter adapter = new SimpleAdapter(context, contents,
R.layout.popup_window_view_item, new String[] { "logo","name"},
new int[] { R.id.share_iv, R.id.share_txt,});
        gridView.setNumColumns(3);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(listener);
    }

}


//实现就可以

CustomPopupWindowView pop = new CustomPopupWindowView(NLPGActivity.this,new ItemClick());  
//显示窗体  
pop.showAtLocation(view,Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);



popup_window_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_window_view_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:gravity="center_horizontal">
    
<GridView 
   android:id="@+id/share_gridview"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent"
   android:gravity="center_horizontal"
   android:layout_marginLeft="10dp"
   android:layout_marginRight="10dp"
   android:background="@drawable/corners_bg"
   android:verticalSpacing="20dp"/>
<TextView 
   android:id="@+id/cancel"
   android:layout_below="@+id/share_gridview"
   android:layout_height="42dp"
   android:layout_width="fill_parent"
   android:background="@drawable/corners_bg"
   android:layout_marginTop="10dp"
   android:gravity="center"
   android:text="@string/cancel"
   android:layout_marginLeft="10dp"
   android:layout_marginRight="10dp"
   android:textSize="20sp"
   android:textColor="@color/light_blue"
   android:layout_marginBottom="16dp"/>
</RelativeLayout>


popup_window_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_margin="20dp">
    
<ImageView 
   android:id="@+id/share_iv"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_marginTop="20dp"
   android:layout_centerHorizontal="true"/>
    <TextView 
        android:id="@+id/share_txt"
        android:layout_below="@+id/share_iv"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_centerHorizontal="true"
        android:textSize="12sp"/>
</RelativeLayout>


fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="600"
        android:fromYDelta="100%p" />
</set>


fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="600"
        android:toYDelta="100%p" />
</set>


style.xml

<style name="mystyle" parent="android:Animation">  
        <item name="@android:windowEnterAnimation">@anim/fade_in</item>  
        <item name="@android:windowExitAnimation">@anim/fade_out</item>
    </style>  


你可能感兴趣的:(PopupWindow)