简单实用Android底部菜单滑出popupwindow案例

附:Demo下载地址.


效果图:



之前写底部滑出菜单总是将简单的问题复杂化,最近由于项目的需要必须要求底部滑出,所以简单写了一下,希望对你们有帮助,不足之处,还请各位多提宝贵意见以便进一步进行修改直接上代码:

主页面代码:

public class MainActivity extends Activity implements View.OnClickListener {

    private View pop;
    private Button mClick;
    private PopupWindow popupWindow;

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

        mClick = findViewById(R.id.click);
        mClick.setOnClickListener(this);

        pop = LayoutInflater.from(this).inflate(R.layout.bottom_pupowindow_layout, null);
        popupWindow = new PopupWindow(pop, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.click:
                if (popupWindow.isShowing()){
                    popupWindow.dismiss();
                }else {
                    showPop(v);
                }
                break;
            case R.id.camera:
                showToastMessage("打开相机");
                break;
            case R.id.photo:
                showToastMessage("打开相册");
                break;
            case R.id.cancel:
                showToastMessage("取消pop成功");
                popupWindow.dismiss();
                break;
        }
    }

    private void showPop(View view) {

        pop.findViewById(R.id.camera).setOnClickListener(this);
        pop.findViewById(R.id.photo).setOnClickListener(this);
        pop.findViewById(R.id.cancel).setOnClickListener(this);

        //设置显示与消失样式
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_go_style);
        //设置点击pop之外的区域不可用,即不能点击外区域将pop消失
        popupWindow.setOutsideTouchable(false);
        //设置BackgroundDrawable
        ColorDrawable cd = new ColorDrawable(0);
        popupWindow.setBackgroundDrawable(cd);
        //设置显示位置
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        //设置DismissListener
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }

    /**
     * 透明度变化
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

    /**
     * Toast提示
     */
    public void showToastMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
布局代码:

<?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:orientation="vertical"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:background="@drawable/pop_background">

    <TextView
        android:id="@+id/camera"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/btn_button_white_selector"
        android:gravity="center"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:text="拍 照"
        android:textColor="#1190dd" />

    <TextView
        android:id="@+id/photo"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="12dp"
        android:background="@drawable/btn_button_white_selector"
        android:gravity="center"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:text="从相册中选择"
        android:textColor="#1190dd" />

    <TextView
        android:id="@+id/cancel"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="12dp"
        android:background="@drawable/btn_button_selector"
        android:gravity="center"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:text="取 消"
        android:textColor="#fff" />
</LinearLayout>

样式资源文件:

<! -  popwidnow显示消失动画 - >
 <style name = “mypopwindow_anim_go_style” >
     <item name = “android:windowEnterAnimation” > @ anim / from_bottom_200
     </ item>         <! - 指定显示的动画xml  - >
 < item name = “android:windowExitAnimation” > @ anim / go_to_bottom_200
     </ item>        <! - 指定消失的动画xml  - >
 </ style>    

这样就可以实现了。


演示下载地址:http //download.csdn.net/download/kqh_2715/10261154.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值