Android PopWindow与GridView练习

练习:1.首先完成如图所示效果

            2.使用PopWindow与GridView完成

            要求点击分享显示如图所示内容,点击取消分享退出。

           并且要求进入退出动画效果为渐入渐出,时间为1秒,且要设置透明效果。

Android PopWindow与GridView练习_第1张图片

布局:

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#8ee1b4"
    tools:context="com.example.administrator.jreduch05.GridView_Activity">

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gv"
        android:numColumns="4"
        >
    GridView>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消分享"
        android:id="@+id/bt1"
        android:layout_below="@+id/gv"
        />
RelativeLayout>
布局效果:

Android PopWindow与GridView练习_第2张图片


布局:

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/iv"
    android:src="@mipmap/ic_launcher"
    android:scaleType="centerCrop"
    />
<TextView
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/tv"
    android:gravity="center"
    android:text="测试"
    />
LinearLayout>

效果:

Android PopWindow与GridView练习_第3张图片


代码:

package com.example.administrator.jreduch05;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PopWindow_Activity extends AppCompatActivity {
    private Button bt;
    private PopupWindow pw;
    private View popView;
    private GridView gv;
    private Button bt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_window_);
        bt=(Button) findViewById(R.id.share);

        popView=getLayoutInflater().inflate(R.layout.activity_grid_view_,null);
        bt1=(Button) popView.findViewById(R.id.bt1);
        gv=(GridView)popView.findViewById(R.id.gv);

        List list =new ArrayList();

        HashMap map=new HashMap();
        map.put("img",R.mipmap.renrou);
        map.put("name","微信");
        list.add(map);

        map=new HashMap();
        map.put("img",R.mipmap.jietu1);
        map.put("name", "QQ");
        list.add(map);

        map=new HashMap();
        map.put("img",R.mipmap.dahai);
        map.put("name", "大海");
        list.add(map);

        map=new HashMap();
        map.put("img",R.mipmap.dianzan);
        map.put("name", "点赞");
        list.add(map);


        SimpleAdapter sa=new SimpleAdapter(this,list,R.layout.gridlayout,
                new String[]{"img","name"},
                new int[]{R.id.iv,R.id.tv}
                );
        gv.setAdapter(sa);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pw = getPopWindow(popView);
            }
        });
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 pw.dismiss();
                    bt1.setVisibility(View.VISIBLE);
                //设置View可见
            }
        });
    }
    public PopupWindow getPopWindow(View view){
        PopupWindow popupWindow=new PopupWindow(view,
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT,true
        );
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.popStyle);
        //设置背景透明度
        WindowManager.LayoutParams ll=
                getWindow().getAttributes();
        ll.alpha=0.6f;
        getWindow().setAttributes(ll);

        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.showAtLocation(bt, Gravity.BOTTOM, 0, 0);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams ll=
                        getWindow().getAttributes();
                ll.alpha=1f;
                getWindow().setAttributes(ll);
                //设置View可见
            }
        });
        return popupWindow;
    }
}
完成效果:

1.                                                                               2.

Android PopWindow与GridView练习_第4张图片Android PopWindow与GridView练习_第5张图片

你可能感兴趣的:(Android)