Android 之从底部弹出PopupWindow

第一步:我们来看看PopupWindow的布局alert_dialog.xml


    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
  >


    android:id="@+id/pop_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
     android:background="#FFE1FF"
     >


    
            android:id="@+id/btn_take_photo"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="20dip"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:text="拍照"
        android:background="#CDC5BF"
        android:textStyle="bold"
         />


            android:id="@+id/btn_pick_photo"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="5dip" 
         android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:text="从相册选择"
         android:background="#CDC5BF"
         android:textStyle="bold"
         />


            android:id="@+id/btn_cancel"
       android:layout_marginLeft="20dip"
       android:layout_marginRight="20dip"
       android:layout_marginTop="15dip" 
  android:layout_marginBottom="15dip"
       android:layout_width="fill_parent"
       android:layout_height="40dp"
       android:text="取消"
       android:background="#CDC5BF"
       android:textColor="#ffffff"
       android:textStyle="bold"
       
        />



//定义style 和动画:

style

<style name="PopupAnimation" parent="android:Animation">
    <item name="android:windowEnterAnimation">@anim/push_bottom_initem>
    <item name="android:windowExitAnimation">@anim/push_bottom_outitem>
style>


anim

push_bottom_in.xml




   


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




push_bottom_out.xml




   


<translate
    android:duration="200"
    android:fromYDelta="0"
    android:toYDelta="50%p"/>




第二步:看看自定义的PopupWindow类

public class SelectPicPopupWindow extends PopupWindow {


private Button btn_take_photo, btn_pick_photo, btn_cancel;
private View mMenuView;
//向外部提方法调用
public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
//取消按钮
btn_cancel.setOnClickListener(new OnClickListener() {


public void onClick(View v) {
//销毁弹出框
dismiss();
}
});
//设置按钮监听
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
//设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LayoutParams.FILL_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.
PopupAnimation);     
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
mMenuView.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y=(int) event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y
dismiss();
}
}
return true;
}
});


}


}

第三步:看看怎么使用:


public class MainActivity extends Activity {


//自定义的弹出框类
SelectPicPopupWindow menuWindow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) this.findViewById(R.id.text);
        //把文字控件添加监听,点击弹出自定义窗口
        tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//实例化SelectPicPopupWindow
menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
//显示窗口,点击哪个控件需要显示
//设置layout在PopupWindow中显示的位置 //点击哪个空降显示pop
menuWindow.showAtLocation(
MainActivity.this.findViewById(R.id.main),       Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); 
}
});
    }
    
    //为弹出窗口实现监听类
    private OnClickListener  itemsOnClick = new OnClickListener(){


public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_take_photo:

//ToDo
break;
case R.id.btn_pick_photo:

//ToDo


break;
default:
break;
}


}
   
    };
    
}


//在alert_dialog.xml 中.为了使按钮的点击效果好看点,我们也可以定义选择器

效果图


Android 之从底部弹出PopupWindow_第1张图片


你可能感兴趣的:(Android开发)