Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

Android开发学习(14)Popupwindow右上角弹出菜单

$
0
0

很多安卓app的右上角都有弹出菜单,今天我们继续丰富我们的计算机app,前几篇博客我们为他增加了历史记录的功能,今天我们使用Popupwindow做一个右上角弹出的按钮。
效果:
pop

布局

按钮主界面

menu_popwindow.xml

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

    <ImageView
            android:layout_width="18sp"
            android:src="@drawable/ic_up"
            android:layout_gravity="right"
            android:layout_marginBottom="-3sp"
            android:layout_marginRight="10sp"
            android:layout_height="18sp"/>
    <ListView
            android:id="@+id/lv_toptitle_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_ground" />


</LinearLayout>

menu_popwindow_item.xml

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

    <ImageView
            android:id="@+id/iv_menu_item"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:src="@drawable/ic_setting" />
    <TextView
            android:id="@+id/tv_menu_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center_vertical"
            android:text="测试"
            android:textColor="@color/background_ground"
            android:textSize="18sp" />


</LinearLayout>

按钮动画

style.xml

<style name="PopupAnimation" parent="android:Animation" mce_bogus="1">
        <item name="android:windowEnterAnimation">@anim/popup_enter</item>
        <item name="android:windowExitAnimation">@anim/popup_exit</item>
    </style>

弹出:
popup_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="0.6" android:toXScale="1.0"
           android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"
           android:pivotY="50%" android:duration="150" />
    <alpha android:interpolator="@android:anim/decelerate_interpolator"
           android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="50" />
</set>

退出:
popup_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
            android:fromXScale="1.0"
            android:toXScale="0.5"
            android:fromYScale="1.0"
            android:toYScale="0.5"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="150" />
    <alpha
            android:interpolator="@android:anim/accelerate_interpolator"
            android:fromAlpha="1.0"
            android:toAlpha="0.0"
            android:duration="150" />
</set>

自定义Popupwindow

MenuPopwindow:

package com.xvshu.android.window;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.xvshu.android.R;
import com.xvshu.android.model.MenuPopwindowBean;

import java.util.List;

/**
 * Created by xvshu on 2017/8/21.
 */
public class MenuPopwindow extends PopupWindow {
    private View conentView;
    private ListView lvContent;
    public MenuPopwindow(Activity context, List<MenuPopwindowBean> list) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.menu_popwindow, null);
        lvContent = (ListView) conentView.findViewById(R.id.lv_toptitle_menu);
        lvContent.setAdapter(new MyAdapter(context, list));
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w / 3-30);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopupAnimation);
    }

    public void setOnItemClick(AdapterView.OnItemClickListener myOnItemClickListener) {
        lvContent.setOnItemClickListener(myOnItemClickListener);
    }

    class MyAdapter extends BaseAdapter {
        private List<MenuPopwindowBean> list;
        private LayoutInflater inflater;
        public MyAdapter(Context context, List<MenuPopwindowBean> list) {
            inflater = LayoutInflater.from(context);
            this.list = list;
        }
        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.menu_popwindow_item, null);
                holder = new Holder();
                holder.ivItem = (ImageView) convertView.findViewById(R.id.iv_menu_item);
                holder.tvItem = (TextView) convertView.findViewById(R.id.tv_menu_item);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.ivItem.setImageResource(list.get(position).getIcon());
            holder.tvItem.setText(list.get(position).getText());
            return convertView;
        }
        class Holder {
            ImageView ivItem;
            TextView tvItem;
        }
    }
    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }
}

绑定

CalcActivity.java

public void  more(View view){
        int[] icons = {R.drawable.ic_setting, R.drawable.ic_delete};
        String[] texts = {"编辑", "删除"};
        List<MenuPopwindowBean> list = new ArrayList<MenuPopwindowBean>();
        MenuPopwindowBean bean = null;
        for (int i = 0; i < icons.length; i++) {
            bean = new MenuPopwindowBean();
            bean.setIcon(icons[i]);
            bean.setText(texts[i]);
            list.add(bean);
        }
        MenuPopwindow pw = new MenuPopwindow(this, list);
        pw.setOnItemClick(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Adapter adapter=parent.getAdapter();
                MenuPopwindowBean thisBean = (MenuPopwindowBean)adapter.getItem(position);
                Log.i("more", "onItemClick: "+thisBean.getText());
            }

        });
        pw.showPopupWindow(findViewById(R.id.button_forward));
    }

点击图片后日志:
log

源码地址:
git_xvshu_android_test

总结

非常感慨,安卓的资源真丰富,也非常感触自己生活的时代,为我们自学提供了当下最好的土壤,最近从朋友圈听到了一句,自己感觉说到了当下这个时代人面临的问题“这个时代正在惩罚不学习的人”,仔细想想,个人感觉这句话,放到哪个时代都是合适的,不断学习,不断更新自己我们才能飞翔的更自由。

作者:xvshu 发表于2017/8/21 15:07:29 原文链接
阅读:0 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>