public class SamplePopup extends Activity {
PopupWindow popup;
//GridView gView;
GridView gView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final LinearLayout musicGrid = (LinearLayout) inflater.inflate(R.layout.gridviewpopup, null, false);
gView = (GridView) musicGrid.findViewById(R.id.gridview);
gView.setAdapter(new ImageAdapter(this));
popup = new PopupWindow(this);
popup.setContentView(musicGrid);
popup.setTouchable(true);
popup.setFocusable(true);
gView.setOnItemClickListener(new Gallery.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{ Log.i("huhudhufhud","");
// popup.showAsDropDown(findViewById(R.id.main));
popup.dismiss();
}
});
final Button popupButton = (Button) findViewById(R.id.popup);
popupButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
popup.setWidth(300);
popup.setHeight(200);
popup.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);
}
});
}
}
粉红色部分就是vpopupwindow要使用的,红色的部分一定要添加 不然点击事件不执行。
layout.gridviewpopup 如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridviewparent"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:gravity="center_horizontal" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="30dp"
android:horizontalSpacing="15dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/popup"
android:scaleType="centerInside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POPUP"
/>
</LinearLayout>
attrs如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
这个呢只是图片的一个外边框背景。
ImageAdapter的扩展
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private int itemBackground;
public ImageAdapter(Context c) {
mContext = c;
//---setting the style---
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return images.length;
}
public Object getItem(int position) {
//return images[position];
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setBackgroundResource(itemBackground);
return imageView;
}
public Integer[] images = {
R.drawable.android,
R.drawable.icon,
R.drawable.android
};
}
标签:PopupWindow,popup,return,int,imageView,使用,ImageAdapter,position,public From: https://blog.51cto.com/u_16166892/6523979