先看效果

android开发 - 自定义  弹出  底部菜单_xml


再看看选择图片库

android开发 - 自定义  弹出  底部菜单_ide_02


OK效果出来了!


现在看步骤


1.新建一个弹出层的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<LinearLayout
android:id="@+id/takepics_layer_popupwin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/color_white"
android:orientation="vertical" >


<TextView
android:id="@+id/takepics_textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:background="@drawable/takepictures_border_1"
android:gravity="center"
android:paddingBottom="6sp"
android:text="@string/str_people_takePictures"
android:textColor="@color/color_black"
android:textSize="16sp" />


<TextView
android:id="@+id/takepics_textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:background="@drawable/takepictures_border_1"
android:gravity="center"
android:paddingBottom="6sp"
android:text="@string/str_people_selectionLibs"
android:textColor="@color/color_black"
android:textSize="16sp" />


<TextView
android:layout_width="match_parent"
android:layout_height="10sp"
android:background="@color/color_gray"/>

<TextView
android:id="@+id/takepics_textview3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginTop="15sp"
android:gravity="center"
android:text="@string/str_people_selcancel"
android:textColor="@color/color_black"
android:textSize="16sp"
android:typeface="monospace" />
</LinearLayout>
</RelativeLayout>

2.新建一个类,继承PopupWindow这个类

3.在主要显示的xml布局的窗口,写一个容器放置这个这个弹出层,如:

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;


public class Person_TakePics_Popupwin extends PopupWindow {
private View selectionMenu;


public Person_TakePics_Popupwin(Activity context,
OnClickListener itemsOnClick) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
selectionMenu = layoutInflater.inflate(
R.layout.takepicsorselectionpics, null);

//拍照按钮
selectionMenu.findViewById(R.id.takepics_textview1).setOnClickListener(
itemsOnClick);

//选择本地库按钮
selectionMenu.findViewById(R.id.takepics_textview2).setOnClickListener(
itemsOnClick);

//取消按钮
selectionMenu.findViewById(R.id.takepics_textview3).setOnClickListener(
itemsOnClick);


// 设置PopupWindow窗口的View
this.setContentView(selectionMenu);
// 设置PopupWindow窗口的宽
this.setWidth(LayoutParams.MATCH_PARENT);
// 设置PopupWindow窗口的高
this.setHeight(LayoutParams.WRAP_CONTENT);
// 设置PopupWindow窗口可点击
this.setFocusable(true);


// 设置一个半透明颜色的背景
ColorDrawable colorDrawable = new ColorDrawable(0xb0000000);
this.setBackgroundDrawable(colorDrawable);


// 监听判断用户触摸的地方,如果在PopupWindow外面,就销毁该PopupWindow
selectionMenu.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int height = selectionMenu.findViewById(
R.id.takepics_layer_popupwin).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
}
}
<?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:background="@color/color_gray"
android:orientation="vertical" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout_menu"
android:layout_gravity="bottom"
android:orientation="vertical">

</LinearLayout></LinearLayout>


4.在主要的activity里操作

private final int IMAGE_RESULT_CODE = 1;// 拍照意图回传值结果码
private final int IMAGE_RESULT_OPEN_CODE = 2;// 选择本地库
private ImageView imageView;// 选择的照片,或者拍照的图片显示
private Person_TakePics_Popupwin person_windowPerson_TakePics_Popupwin;// popup弹出框


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal_activity);

imageView = (ImageView) this.findViewById(R.id.icon_people_imgview);
TextView textview_clickTextView = (TextView) this
.findViewById(R.id.click_for_sel_person);
textview_clickTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//实例化PopupWindow
person_windowPerson_TakePics_Popupwin = new Person_TakePics_Popupwin(
Personal_Activity.this, itemsOnClick);
//显示窗口,设置layout在PopupWindow中显示的位置
person_windowPerson_TakePics_Popupwin.showAtLocation(
Personal_Activity.this
.findViewById(R.id.linearLayout_menu),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);


}
});
}



private View.OnClickListener itemsOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
person_windowPerson_TakePics_Popupwin.dismiss();
switch (v.getId()) {
case R.id.takepics_textview1:
// 拍照意图
Intent intent = new Intent(
android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, IMAGE_RESULT_CODE);
break;
case R.id.takepics_textview2:
// 选择图片库的图片
Intent intent2 = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent2, IMAGE_RESULT_OPEN_CODE);
break;
}
}
};
// activity返回操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 表示拍照返回后的显示结果
if (requestCode == 1 && resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
// 表示选择本地图片库后的图片显示结果
} else if (requestCode == 2 && resultCode == RESULT_OK) {
imageView.setImageURI(data.getData());
}
}}