30-popwindow

本文介绍如何使用Android中的PopupWindow组件创建弹出窗口,并详细展示了如何通过自定义布局、绑定数据源及设置动画来增强用户体验。此外,还提供了完整的代码示例。

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

/PopupWindow/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:id="@+id/main"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:onClick="openPopWindow"
         />

</LinearLayout>

/PopupWindow/res/layout/popwindow.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/bg"
    >

    <GridView
        android:layout_width="match_parent"
    	android:layout_height="match_parent"
        android:numColumns="4"  <span style="color:#FF0000;">每行个数</span>
        android:horizontalSpacing="10dp"  <span style="color:#FF0000;">间距</span>
        android:verticalSpacing="10dp"
        android:id="@+id/gridView"
        />

</LinearLayout>
/PopupWindow/res/layout/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:layout_width="wrap_content"
   		 android:layout_height="wrap_content"
         android:id="@+id/imageView"
        />
	<TextView 
	    android:layout_width="fill_parent"
   		android:layout_height="wrap_content"
   		android:gravity="center"
   		android:textSize="16sp"
   		android:textColor="#000099"
        android:id="@+id/textView"
	    />
</LinearLayout>
/PopupWindow/src/cn/itcast/popwindow/MainActivity.java

package cn.itcast.popwindow;

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 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
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        View contentView = getLayoutInflater().inflate(R.layout.popwindow, null);
        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.setBackgroundDrawable(new BitmapDrawable());
    	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();//关闭
			//....
		}    	
    }
    
    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;
	}

	public void openPopWindow(View v){
		//Gravity.BOTTOM 底部  Gravity.CENTER 中部
    	popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    	//popupWindow.showAsDropDown(v);//相对某个控件的位置正左下方,无偏移
    	//popupWindow.showAsDropDown(anchor, xoff, yoff);相对某个控件的位置正左下方,有偏移
    }
}

/PopupWindow/res/anim/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.7"
        android:toAlpha="1.0" 
        android:duration="300"
        /> 	
</set>

 /PopupWindow/res/anim/out.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="3000"
        />
 	<alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.5" 
        android:duration="2000"
        /> 	
</set>


/PopupWindow/res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> 
    <style name="animation">
        <item name="android:windowEnterAnimation">@anim/enter</item> 
	    <item name="android:windowExitAnimation">@anim/out</item> 
    </style> 
</resources>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值