运用android PopupWindow实现从底部弹出或滑出选择菜单或窗口
没图没真相。。。
首先是Xml代码
<?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="@drawable/exit_dialog_bg"
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageButton android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_85"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享到微博"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_86"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收藏"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_87"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享到微博"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_85"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享到微博"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_86"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收藏"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_87"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享到微博"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_85"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享到微博"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_86"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收藏"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/emoji_87"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享到微博"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
<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:background="@drawable/btn_click"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
然后是继承PopupWindow的类代码:
package com.stcyclub.testhtmlgetlinkman;
import android.app.Activity;
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.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class SelectPicPopupWindow extends PopupWindow {
private Button 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.mult, null);
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.anim.in_from_botton);
//实例化一个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<height){
dismiss();
}
}
return true;
}
});
}
}
最后是activity代码,因为同时在练习其他知识,所有这块包含其他代码
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- 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);
- //显示窗口
- menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
- }
- });
- }
- //为弹出窗口实现监听类
- private OnClickListener itemsOnClick = new OnClickListener(){
- public void onClick(View v) {
- menuWindow.dismiss();
- switch (v.getId()) {
- default:
- break;
- }
- }
- };
- }