android popupwindow,调用本地的图库,照相

我们写项目的时候又是要自定义弹出美观的悬浮操作窗口,这里我们可以通过popUpWindow来时现:以下是点击按钮弹出popUpWindow用于选择本地上传图片的选择操作:
LayoutInflater mLayoutInflater = (LayoutInflater) activity
.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
View headwindow = mLayoutInflater.inflate(你要显示几界面的ID(R.id.picchoose), null);
PopupWindow mPopupWindow = new PopupWindow(headwindow,
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
mPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
headalbum = (Button) headwindow.findViewById(R.id.button_headalbum);
headcamera = (Button) headwindow.findViewById(R.id.button_headcamera);
headquit = (Button) headwindow.findViewById(R.id.button_headquit);

headalbum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 选择相册的图片
Intent mIntent1 = new Intent(Intent.ACTION_GET_CONTENT);
mIntent1.addCategory(Intent.CATEGORY_OPENABLE);
mIntent1.setType("image/*");
activity.startActivityForResult(mIntent1, ApplicationConstant.FROM_GALLERY);
mPopupWindow.dismiss();
}
});
headcamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 选择照相图片
Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
// 获取这个图片的URI
File photofile = new File(photopath);
Uri originalUri = Uri.fromFile(photofile);// 这是个实例变量,方便下面获取图片的时候用
mIntent.putExtra(MediaStore.EXTRA_OUTPUT, originalUri);
activity.startActivityForResult(mIntent, ApplicationConstant.FROM_CAMERA);
mPopupWindow.dismiss();
}
});
headquit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
mPopupWindow.dismiss();
}
});

xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#a0000000"
android:gravity="center_horizontal"
android:paddingTop="15dip" android:paddingBottom = "25dip"
>

<Button android:id="@+id/button_headcamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_headcamera"/>
<Button android:id="@+id/button_headalbum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_headalbum"
android:layout_marginTop = "5px"/>
<Button android:id="@+id/button_headquit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_headquit"
android:layout_marginTop = "5px"/>
</LinearLayout>
当然界面你也可以定义成其他的方式这个靠你自由的发挥了。

调用本地的图库后可以在activity中重新写onActivityResult这个方法来获取你选择的图片
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嗨!对于你关于 Android PopupWindow 的问题,我可以给你一些基本的信息。PopupWindowAndroid 的一个弹出式窗口,它可以覆盖在其他视图之上,提供一种临时性的用户界面。你可以使用 PopupWindow 来显示额外的内容、菜单或者交互提示。 要创建一个 PopupWindow,首先你需要实例化一个 PopupWindow 对象,并为其设置一些基本属性,例如宽度、高度、背景等。然后,你可以将任何视图或布局添加到 PopupWindow ,并通过设置位置参数来控制它的显示位置。 下面是一个简单的示例代码,展示如何创建和显示一个 PopupWindow: ```java // 创建一个 PopupWindow 对象 PopupWindow popupWindow = new PopupWindow(context); // 设置 PopupWindow 的宽度和高度 popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置 PopupWindow 的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 设置 PopupWindow 的背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示 PopupWindow popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0); ``` 在上面的示例,我们创建了一个 PopupWindow 对象,并设置了宽度和高度为包裹内容。然后,我们通过调用 `setContentView` 方法将一个自定义的布局文件 `R.layout.popup_layout` 添加到 PopupWindow 。最后,我们使用 `showAtLocation` 方法将 PopupWindow 显示在屏幕央。 希望这些信息对你有帮助!如果你对 PopupWindow 有更多的问题,或者需要更详细的示例代码,请随时告诉我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值