popupwindow实现类似ActionBar右侧更多效果

来源:互联网 发布:linux客户端是什么意思 编辑:程序博客网 时间:2024/06/03 02:51

使用popupWindow实现标题右侧更多的效果

下面简单介绍一下popupWindow:

A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity

popupwindow 能够用来展示一个任意的view,它是悬浮在当前activity上边的一个浮动的容器

这是开发文档中的介绍,通过这句话我们可以简单的把popupWindow当成一个显示view的容器就行了.


自定义标题栏在前边已经写了,这里就不写了,直接开始写popupwindow了

1.写一个customPopWindow类

package cn.ljj.pop;import android.app.Activity;import android.content.Context;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.util.Log;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.View.MeasureSpec;import android.view.ViewGroup.LayoutParams;import android.view.WindowManager;import android.widget.PopupWindow;public class CustomPopWindow {    /** popupWindow背景 */    protected Drawable mBackground = null;    protected Context mContext;    /**     * 加载在popupWindow上的view     */    protected View mRootView;    protected PopupWindow mWindow;    /**     * 触摸外部是否关闭popupWindow     */    private boolean outsideDismiss = true;    /**     * 实例化构造方法。     *      * @param context 上下文内容     * @param outsideDismiss 是否允许点击外部关闭popup     */    public CustomPopWindow(Context context, boolean outsideDismissFlag) {        mContext = context;        mWindow = new PopupWindow(context);        this.outsideDismiss = outsideDismissFlag;        // 根据触摸点来设置outsideDismiss的值        mWindow.setTouchInterceptor(new View.OnTouchListener() {            public boolean onTouch(View view, MotionEvent event) {                int viewWidth = view.getWidth();                int viewHeight = view.getHeight();                float x = event.getX();                float y = event.getY();                Log.e("x,y", x + "--" + y);                Log.e("w,h", viewWidth + "--" + viewHeight);                if (x < 0 || x > viewWidth || y < 0 || y > viewHeight) {                    return !outsideDismiss;                }                return false;            }        });        mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);    }    /**     * 实例化构造方法     *      * @param context     */    public CustomPopWindow(Context context) {        mContext = context;        mWindow = new PopupWindow(context);        mWindow.setTouchInterceptor(new View.OnTouchListener() {            public boolean onTouch(View view, MotionEvent event) {                return false;            }        });        mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);    }    /**     * 关闭已经打开的popupWindow     */    public void dismiss() {        if (mWindow.isShowing()) {            mWindow.dismiss();        }    }    /**     * 设置popupWindow的动画style     *      * @param resId 在style中可以使用windowEnterAnimation和windowExitAnimation这两个属性加载各自的动画     */    public void setAnimationStyle(int resId) {        if (null != mWindow) {            mWindow.setAnimationStyle(resId);        }    }    /**     * 设置窗口软键盘的交互模式     *      * @param type 窗口与软键盘交互的类型     */    public void setSoftInputMode(int type) {        mWindow.setSoftInputMode(type);    }    /**     * 更改popupWindow的键盘弹出模式     *      * @param type 更改popupWindow的键盘弹出类型     */    public void setImputMethodMode(int type) {        mWindow.setInputMethodMode(type);    }    /**     * show popupWindow之前背景的设置     */    protected void preShow() {        if (mRootView == null) {            throw new IllegalStateException(                    "setContentView was not called with a view to display.");        }        if (mBackground == null)            mWindow.setBackgroundDrawable(new BitmapDrawable(mContext.getResources()));        else {            mWindow.setBackgroundDrawable(mBackground);        }        mWindow.setFocusable(true);        mWindow.setOutsideTouchable(true);        mWindow.setTouchable(true);    }    /**     * 设置显示PopupWindow的位置位于View的左下方     *      * @param view     */    public void showAsDropDown(View view) {        preShow();        mWindow.showAsDropDown(view);    }    /**     * 设置显示PopupWindow的位置位于View的下方     *      * @param view     * @param x x偏移量     * @param y y偏移量     */    public void showAsDropDown(View view, int x, int y) {        preShow();        mWindow.showAsDropDown(view, x, y);    }    /**     * 窗口方向为向上     *      * @param view     */    public void showPopUpward(View view) {        preShow();        int width = -getViewWidth();        int height = -getViewHeight();        mWindow.showAsDropDown(view, width, height);    }    /**     * 向上显示窗口     */    public void showPopUpward(View view, int x, int y) {        preShow();        int width = -getViewWidth() + x;        int height = -getViewHeight() + y;        mWindow.showAsDropDown(view, width, height);    }    /**     * popupWindow显示在具体位置     *      * @param parent 父布局     * @param gravity 方位     * @param x x偏移量     * @param y y偏移量     */    public void showAtLocation(View parent, int gravity, int x, int y) {        preShow();        if (null == parent) {            if (mContext instanceof Activity) {                Activity act = (Activity) mContext;                parent = act.getWindow().getDecorView();            }        }        mWindow.showAtLocation(parent, gravity, x, y);    }    /**     * 设置popupWindow的高度     *      * @param height     */    public void setHeight(int height) {        mWindow.setHeight(height);    }    /**     * 获取popupWindow的高度     */    public int getHeight() {        return mWindow.getHeight();    }    /**     * 设置popupWindow的宽度     *      * @param width     */    public void setWidth(int width) {        mWindow.setWidth(width);    }    public float getElevation() {        return mWindow.getElevation();    }    /**     * 获取popupWindow的宽度     */    public int getWidth() {        return mWindow.getWidth();    }    /**     * 获取布局的原始测量高度     */    public int getViewHeight() {        return mRootView == null ? 0 : mRootView.getMeasuredHeight();    }    /**     * 获取布局的原始测量高度     */    public int getViewWidth() {        return mRootView == null ? 0 : mRootView.getMeasuredWidth();    }    /**     * 设置背景图片     *      * @param drawable     */    public void setBackgroundDrawable(Drawable drawable) {        mBackground = drawable;    }    /**     * 加载xml 布局文件     *      * @param res 布局的资源文件     */    public void setContentView(int res) {        View view = ((LayoutInflater) mContext                .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(                res, null);        view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));        setContentView(view);    }    /**     * @param view     */    public void setContentView(View view) {        mRootView = view;        view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));        mWindow.setContentView(mRootView);        mWindow.setWidth(LayoutParams.WRAP_CONTENT);        mWindow.setHeight(LayoutParams.WRAP_CONTENT);    }    /**     * 获取设置在popupWindow上的View     */    public View getContentView() {        return mRootView;    }    /**     * 关闭popupWindow的监听     *      * @param listener     */    public void setOnDismissListener(PopupWindow.OnDismissListener listener) {        mWindow.setOnDismissListener(listener);    }    /**     * popupWindow是否正在打开     */    public boolean isShowing() {        return mWindow.isShowing();    }    /**     * 更新popupWindow 将会重新调用:     * PopupWindow.setClippingEnabled(boolean),PopupWindow.setFocusable(boolean)},     * PopupWindow.setIgnoreCheekPress(), PopupWindow.setInputMethodMode(int)},     * PopupWindow.setTouchable(boolean), PopupWindow.setAnimationStyle(int)}.     */    public void update() {        mWindow.update();    }}

2.标题右侧按钮点击事件

 tv.setTextLeftClickListener(new View.OnClickListener() {            public void onClick(View v) {                CustomPopWindow customPopWindow = new CustomPopWindow(MainActivity.this, true);                customPopWindow.setContentView(LayoutInflater.from(MainActivity.this).inflate(R.layout.more, null));                customPopWindow.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));                customPopWindow.showAsDropDown(tv, -80, 5);                Log.e("elevation", customPopWindow.getElevation() + "");            }});

3.布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@drawable/popbc"    android:orientation="vertical" >    <TextView        android:id="@+id/line1"        android:layout_width="wrap_content"        android:layout_height="20dp"        android:text="第一行"        android:textColor="#fff" />    <View        android:layout_width="match_parent"        android:layout_height="0.5dp"        android:background="#fff" />    <TextView        android:id="@+id/line2"        android:layout_width="wrap_content"        android:layout_height="20dp"        android:text="第二行"        android:textColor="#fff" />    <View        android:layout_width="match_parent"        android:layout_height="0.5dp"        android:background="#fff" />    <TextView        android:id="@+id/line3"        android:layout_width="wrap_content"        android:layout_height="20dp"        android:text="第三行"        android:textColor="#fff" /></LinearLayout>

效果如图:这个图是没运行的时候,运行时就出现在按钮左下方了

4好了,以上就是popupwindow的简单实用了标题栏右侧更多的实现了




0 0