2011.07.05(2)——— android PopupWindow

quickisbest 2011-07-05

2011.07.05(2)———androidPopupWindow

参考:http://www.eoeandroid.com/thread-48051-1-1.html

http://disanji.net/2010/12/08/android-popupwindow-2/

http://www.iteye.com/topic/604462

1、新写一个layout文件作为popupwindow的布局文件

<?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:gravity="center"
    android:background="#c0000000"> 
    <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/popupwindow"> 
    <LinearLayout 
    	android:layout_width="fill_parent"  
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_margin="5dip">    
        <TextView 
	    	android:text="课程:"
	    	android:layout_width="wrap_content" 
	        android:layout_height="wrap_content"
	    /> 
	    <EditText 
	    	android:id="@+id/popupwindow_class"  
	        android:layout_width="200dip" 
	        android:layout_height="wrap_content"
	        />
    </LinearLayout>
    <LinearLayout 
    	android:layout_width="fill_parent"  
        android:layout_height="wrap_content" 
        android:gravity="center">  
        <Button 
        	android:text="添加" 
        	android:id="@+id/popupwindow_add"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
       	</Button>  
        <Button 
        	android:text="取消" 
        	android:id="@+id/prpupwindow_cancel"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
      	</Button>  
    </LinearLayout>  
    </LinearLayout>
</LinearLayout>

2、布局文件当中android:background="@drawable/popupwindow"的边框圆角背景

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient 
	        android:startColor="#c0000000" 
	        android:endColor="#c0000000"
            android:angle="90" /><!--背景颜色渐变 -->
        <stroke 
	        android:dashWidth="2dp" 
	        android:dashGap="2dp"
            android:width="2dp" 
            android:color="#FF00ff00"></stroke>
        <!--描边 -->
        <corners 
        	android:bottomRightRadius="5dp"
            android:bottomLeftRadius="5dp" 
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp" /><!--设置圆角-->
</shape>

3、显示popupwindow时的动态效果

res/anim/popuwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="0.0" 
        android:toXScale="1.0" 
        android:fromYScale="0.0"
        android:toYScale="1.0" 
        android:pivotX="50%" 
        android:pivotY="50%"
        android:fillAfter="true" 
        android:duration="3000" />
	 <alpha 
        android:fromAlpha="0.0" 
        android:toAlpha="1.0" 
        android:duration="3000" />
</set>

4、代码

/**
     * 添加课程的popupwindow
     * @param v
     */
    private void initPopupWindow(View v){
        final View view = this.getLayoutInflater().inflate(R.layout.popupwindow, null);
        popupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT, true);
        Button btnOK=(Button)view.findViewById(R.id.popupwindow_add);
        btnOK.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                EditText et=(EditText)view.findViewById(R.id.popupwindow_class);
                。。。。。。//一些处理
        });
 
      //Cancel按钮及其处理事件
        Button btnCancel=(Button)view.findViewById(R.id.prpupwindow_cancel);
        btnCancel.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                popupWindow.dismiss();//关闭
            }
        });
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
        popupWindow.setAnimationStyle(R.anim.popupwindow);
        
    }

相关推荐