Android学习笔记_35_PopupWindow泡泡窗口的实现及GridView应用

本文详细介绍了如何创建和使用PopupWindow,包括其基本概念、界面创建、动画设置及后台代码实现。主要内容涵盖PopupWindow的基本用法、界面布局、动画效果及与Activity的交互,适合Android开发人员学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1、PopupWindow是一个可以显示在当前Activity之上的浮动容器,PopupWindow弹出的位置是能够改变的,按照有无偏移量,可以分为无偏移和有便宜两种;按照参照对象的不同又可以分为两种:相对某个控件(Anchor锚点)的位置和在父容器内部的相对位置。

  显示PopupWindow的方法:

 

 

 

showAsDropDown(Viewanchor) //相对某个控件的位置(正下方),无偏移
showAsDropDown(Viewanchor, int xoff, int yoff) //相对某个控件的位置,有偏移,xoff X轴的偏移量,yoff Y轴的偏移量
showAtLocation(Viewparent, int gravity, int x, int y) //在父容器的什么位置,gravity为相对位置,如:正中央Gravity.CENTER、下方Gravity.BOTTOM、
Gravity.Right|Gravity.BOTTOM右下方等,后面两个参数为x/y轴的偏移量。

    2、创建泡泡窗口的界面:

<?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="@drawable/bg"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="10dp"
        android:numColumns="4"
        android:verticalSpacing="10dp" />
</LinearLayout>

  为GridView控件设置背景色,在res/drawable目录创建bg.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="270"
        android:endColor="#1DC9CD"
        android:startColor="#A2E0FB" />

    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

</shape>

  泡泡窗口由图片和文字组成,因此为它的项设置布局界面grid_item.xml文件:

<?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:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#000099"
        android:textSize="16sp" />

</LinearLayout>

  3、为泡泡窗口设置动画效果:

  设置动画样式:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">  
    </style>
    <style name="AppTheme" parent="AppBaseTheme">
    </style>
    <style name="animation">
        <item name="android:windowEnterAnimation">@anim/enter</item>
        <item name="android:windowExitAnimation">@anim/out</item>
   </style>
</resources>

  动画样式需要在res/anim下创建,需要创建进入(enter.xml)和退出(out.xml)两个动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <!-- 设置平移,现在只需要在Y轴上移动 -->
    <translate
        android:duration="500"
        android:fromYDelta="100%p"
        android:toYDelta="0" />
    <!-- 设置透明度 -->
    <alpha
        android:duration="300"
        android:fromAlpha="0.7"
        android:toAlpha="1.0" />

</set>

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="3000"
        android:fromYDelta="0"
        android:toYDelta="100%p" />

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.5" />
</set>

  4、后台代码实现:

package com.example.popupwindow;

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

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
    PopupWindow popupWindow;
    View parent;
    private int[] images = { R.drawable.i1, R.drawable.i2, R.drawable.i3,
            R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,
            R.drawable.i8 };
    private String[] names = { "搜索", "文件管理", "下载管理", "全屏", "网址", "书签", "加入书签",
            "分享页面" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // LayoutInflater mLayoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
        //通过LayoutInflater获取布局界面
        View contentView = getLayoutInflater().inflate(R.layout.popupwin, null);
        //查找popupwin界面的GridView控件
        GridView gridView = (GridView) contentView.findViewById(R.id.gridView);
        //填充数据
        gridView.setAdapter(getAdapter());
        //设置点击事件
        gridView.setOnItemClickListener(new ItemClickListener());

        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setFocusable(true);// 取得焦点
        // 点击空白的地方关闭PopupWindow
        popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
        //给popupWindow窗口加入动画效果
        popupWindow.setAnimationStyle(R.style.animation);
        parent = this.findViewById(R.id.main);
    }

    private final class ItemClickListener implements OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            if (popupWindow.isShowing())
                popupWindow.dismiss();// 关闭
            // ....
        }
    }
    
    //将图片和名称字段对应到视图界面grid_view的图像和文本上
    private ListAdapter getAdapter() {
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < images.length; i++) {
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("image", images[i]);
            item.put("name", names[i]);
            data.add(item);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
                R.layout.grid_item, new String[] { "image", "name" },
                new int[] { R.id.imageView, R.id.textView });
        return simpleAdapter;

    }
    //打开PopupWindow窗口
    public void openPopWindow(View v) {
        //在父窗口的底部显示,没有偏移量
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值