今天我们学习如何实现PopupWindow泡泡窗口,效果类似于弹出菜单,但是实现方式截然不同,下面给出该场景的案例:

一、案例代码

1.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android.popupwindow"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".PopupWindowActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

2.string.xml

<resources>    <string name="app_name">泡泡窗口</string>    <string name="button">打开PopupWindow</string></resources>

3.主界面布局文件:main.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:orientation="vertical"    android:id="@+id/main" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="openPopupWindow"        android:text="@string/button" /></LinearLayout>

4.PopupWindow布局文件:popupwindow.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:orientation="vertical"    android:background="@drawable/background" >    <!-- <TextView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="我是PopupWindow"/> -->                <GridView            android:id="@+id/gridView"            android:layout_width="match_parent"            android:layout_height="match_parent"             android:numColumns="4"            android:verticalSpacing="10dp"            android:horizontalSpacing="10dp"/>            </LinearLayout>

5.GridViewItem布局文件: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:orientation="vertical"    android:gravity="center" >    <ImageView         android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <TextView         android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:textSize="16sp"        android:textColor="#000099"/></LinearLayout>

6.PopupWindow弹进动画文件:enter.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false" >    <translate         android:fromYDelta="100%p"        android:toYDelta="0"        android:duration="500" />        <alpha        android:fromAlpha="0.5"         android:toAlpha="1.0"        android:duration="300" /></set>

7.PopupWindow弹出动画文件:exit.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false" >    <translate         android:fromYDelta="0"        android:toYDelta="100%p"        android:duration="500" />        <alpha        android:fromAlpha="1.0"         android:toAlpha="0.5"        android:duration="300" /></set>

8.PopupWindowActivity.java

package com.android.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.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 PopupWindowActivity extends Activity {    private PopupWindow popupWindow;    private View parent;        // 设置GridView Item图片资源    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};        // 设置GridView Item名称资源    private String[] names = {            "搜索",            "文件",            "下载",            "全屏",            "网址",            "书签",            "加入",            "分享"    };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        // 构建PopupWindow内容视图        View contentView = getLayoutInflater().inflate(R.layout.popupwindow, null);        GridView gridView = (GridView) contentView.findViewById(R.id.gridView);        gridView.setAdapter(getAdapter());        gridView.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position,                    long id) {                if(popupWindow.isShowing()) {                    popupWindow.dismiss();                }            }        });                // 初始化PopupWindow控件        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);        // 允许PopupWindow获取焦点        popupWindow.setFocusable(true);        // 设置PopupWindow背景图        popupWindow.setBackgroundDrawable(new BitmapDrawable());        // 设置PopupWindow弹进弹出动画效果        popupWindow.setAnimationStyle(R.style.animation);        // 找到主界面布局文件        parent = findViewById(R.id.main);    }    /**     * 构建用于填充GridView数据条目的适配器     * @return ListAdapter     */    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 adapter = new SimpleAdapter(this, data, R.layout.grid_item,                 new String[]{"image", "name"}, new int[]{R.id.imageView, R.id.textView});        return adapter;    }    /**     * 设置在页面底端显示PopupWindow     * @param v     */    public void openPopupWindow(View v) {        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);    }}

注意:要将、、、、、、、存放到drawable-hdpi文件夹下。

二、案例效果


三、代码下载:http://download.csdn.net/detail/leverage_1229/5470815








更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android实现气泡布局/弹窗效果 气泡尖角方向及偏移量可控
  6. 在eclipse中将android工程打包生成apk文件
  7. Android(安卓)ListView单选CheckBox
  8. android调用其他人的so文件
  9. Android(安卓)自定义View (一)

随机推荐

  1. android启动方式
  2. Synchronization in Android
  3. Android Studio 下无法调用org.apache。
  4. 2014.01.10 ——— android listview 记
  5. 45个android实例源码,很好很强大
  6. Android(安卓)视频缩放/放大
  7. 获取显示窗口警告 android The method ge
  8. Android下载速度计算
  9. layout居中
  10. [置顶] android canvas 绘图笔记