Popupwindow 的简单使用

public class Popupwindow extends PopupWindow implements OnClickListener, OnTouchListener {


private View mMenuView;
private Button scene_camera_btn, scene_photo_btns, cancel_btns;


@SuppressLint("InflateParams")
public Popupwindow(Context context, OnClickListener onClickListener) {

super(context);

//加载布局文件

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


mMenuView = inflater.inflate(R.layout.popupwindow_driver, null);

Button mCamera = (Button) mMenuView.findViewById(R.id.btn_take_photo);
Button mPhoto = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
Button mCancle = (Button) mMenuView.findViewById(R.id.btn_cancel);

//设置点击事件

mCamera.setOnClickListener(onClickListener);

mCancle.setOnClickListener(this);
mPhoto.setOnClickListener(onClickListener);

this.setContentView(mMenuView);
this.setWidth(LayoutParams.MATCH_PARENT);
this.setHeight(LayoutParams.MATCH_PARENT);
// 设置SelectPicPopupWindow弹出窗体可点击

this.setFocusable(true);

                //设置popupwindow 动画效果

this.setAnimationStyle(R.style.popwin_anim_style);
ColorDrawable dw = new ColorDrawable(0xb0000000);
this.setBackgroundDrawable(dw);
mMenuView.setOnTouchListener(this);
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_cancel:
dismiss();
break;
}
}

//点击空白处消失
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
dismiss();
}
return true;
}

}


Popupwindow 使用方法


首先初始化  Popupwindow popupwindow = new Popupwindow(getActivity(), new mOnClickListener());

创建mOnClickListener监听事件

public class mOnClickListener implements OnClickListener {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.driver_header:
// if (!isLogin())
// return;

// popupwindow 出来的位置
popupwindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
break;
case R.id.btn_take_photo:// 拍照


String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
photoUri = getActivity().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
photoUri);


startActivityForResult(intent, 2);
popupwindow.dismiss();
} else {
ToastUtil.show(getActivity(), "请确认已经插入SD卡", 0);
}


break;
case R.id.btn_pick_photo:// 相册
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(intent, 1);
popupwindow.dismiss();
break;
}
}
}

// popupwindowv布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   
    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"  
  >  
  
<LinearLayout   
    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="#FFFFFF"  
     >  
  
      
    <Button  
        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="wrap_content"  
        android:text="拍照"  
       android:background="@drawable/button_norma"  
        android:textStyle="bold"  
         />  
  
    <Button  
        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="wrap_content"  
        android:text="从相册选择"  
       android:background="@drawable/button_norma"  
         android:textStyle="bold"  
         />  
  
    <Button  
        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="wrap_content"  
       android:text="取消"  
       
       android:textColor="#ffffff"  
       android:textStyle="bold"  
         
        />  
</LinearLayout>  
</RelativeLayout>  


//popupwindow 动画style

    <style name="popwin_anim_style">
        <item name="android:windowEnterAnimation">@anim/popupwindow_show</item>
        <item name="android:windowExitAnimation">@anim/popupwindow_out</item>
    </style>

//popupwindow 出现和消失动画

//popupwindow_show

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">    
    <translate    
        android:fromXDelta="0"    
        android:toXDelta="0"    
        android:fromYDelta="120"    
        android:toYDelta="0"    
        android:duration="500" />    
</set>  

//popupwindow_out

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">    
    <translate    
        android:fromXDelta="0"    
        android:toXDelta="0"    
        android:fromYDelta="0"    
        android:toYDelta="120"    
        android:duration="500" />    
</set>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值