高级控件 popwindow 与gridview的组合应用

Gridview 的布局设置

<GridView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"//4列
android:id="@+id/gg"
android:background="#cf1818">//背景颜色
</GridView>
<Button//按钮用来关闭gridview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消分享"
android:id="@+id/popb"
android:gravity="center"
android:layout_below="@+id/gg"/>
Popwindo的布局
<Button//简单的一个按钮,用来弹出gridview界面
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bb"
android:text="分享"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
main函数的书写
public class Main2Activity extends AppCompatActivity {
private Button bt;
private PopupWindow popupWindow;
private GridView gridView;
private View view;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_gild);
view=getLayoutInflater().inflate(R.layout.grid,null);//得到gridview的那个布局界面
gridView=(GridView)view.findViewById(R.id.gg);//得到布局界面的id 进行监听
/*
往gridvie中存放数据
*/
List list=new ArrayList();
Map map=new HashMap();
map.put("img", R.mipmap.a);
map.put("name", "朋友圈");
list.add(map);
map=new HashMap();
map.put("img", R.mipmap.a);
map.put("name", "朋友圈");
list.add(map);
map=new HashMap();
map.put("img", R.mipmap.a);
map.put("name", "朋友圈");
list.add(map);
map=new HashMap();
map.put("img", R.mipmap.a);
map.put("name", "朋友圈");
list.add(map);
map=new HashMap();
map.put("img", R.mipmap.a);
map.put("name", "朋友圈");
list.add(map);
map=new HashMap();
map.put("img", R.mipmap.a);
map.put("name", "朋友圈");
list.add(map);
map=new HashMap();
map.put("img", R.mipmap.a);
map.put("name", "朋友圈");
list.add(map);
SimpleAdapter ss=new SimpleAdapter(this,list,R.layout.grid_examp,
new String[]{"img","name"},new int[]{R.id.imm,R.id.tt1});//gridview的适配器,以你写好的模板格式填充数据
gridView.setAdapter(ss); bt = (Button) findViewById(R.id.bb);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow=getPopupWindow(view); //调用popwindow 控件
//设置按钮隐藏
bt.setVisibility(View.GONE);
}
});
button=(Button)view.findViewById(R.id.popb);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();//gridview中的取消按钮监听,点击取消popwinodw界面
}
});
}
//定义一个内部类 实现popwindow视图
public PopupWindow getPopupWindow(View view){
popupWindow=new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(false);//点击pop外部是否取消
popupWindow.setBackgroundDrawable(new ColorDrawable());//给popwindow一个空背景
popupWindow.setAnimationStyle(R.style.pop2);//设置他的出现风格,这里是出现速度 ,详情见下面代码
//设置出现与消失时的背景透明度
WindowManager.LayoutParams ll=
getWindow().getAttributes();
ll.alpha=0.6f;
getWindow().setAttributes(ll);
popupWindow.showAtLocation(bt, Gravity.BOTTOM, 0, 0);//从底部出现
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//设置背景透明度
WindowManager.LayoutParams ll=
getWindow().getAttributes();
ll.alpha=1f; //要与上边的ll形成对比 才能看到较明显的效果
getWindow().setAttributes(ll);
bt.setVisibility(View.VISIBLE);//让隐藏的按钮再出来
}
});
return popupWindow;
}
}
附 style 代码
<style name="pop2"> 
<item name="android:windowEnterAnimation">
@anim/pop_enter2
</item>
<item name="android:windowExitAnimation">
@anim/pop_exit2
</item>
</style>
其中pop——enter2和pop-exit2分别为出现消失的设置
pop——enter2
<translate 
android:duration="3000"
android:fromYDelta="100%p"
android:toYDelta="0">
</translate>
<alpha
android:fromAlpha="0"
android:duration="3000"
android:toAlpha="1"/>
pop-exit2
<translate
android:duration="3000"
android:fromYDelta="0"
android:toYDelta="100%p"
></translate>
<alpha
android:fromAlpha="1"
android:duration="3000"
android:toAlpha="0"/>





原文地址:https://www.cnblogs.com/leirenyuan/p/5730959.html