记一次andorid 底部按钮实现方式 PopupWindow实现

本人是一个新手 代码不好之处请指出 勿喷

直接上代码
缺点:需要再每个展示底部按钮的界面的xml的最外层的布局组件中加上
android:layout_marginBottom=“55dp”
不加会导致最下面的被隐藏;

效果图 此界面只是做展示效果用

在这里插入图片描述

private PopupWindow pop;

public void loadFooterMenu() {
        View bottomView = activity.getLayoutInflater().inflate(R.layout.activity_footer_menu, null);
        //底部菜单加载
        myInfoBtn = bottomView.findViewById(R.id.footer_my_info);
        mainBtn = bottomView.findViewById(R.id.footer_home);
        pop = new PopupWindow(bottomView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT );
        pop.setContentView(bottomView);
        pop.setBackgroundDrawable(new ColorDrawable(0x00000000));

        //设置点击其他地方不会消失
        pop.setOutsideTouchable(false);
        //设置不会聚焦
        pop.setFocusable(false);
        pop.setWindowLayoutType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 1f;//透明度 1 完全透明
        activity.getWindow().setAttributes(lp);
        pop.setAnimationStyle(R.style.main_menu_photo_anim);
        pop.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        //设置按钮点击事件
        if (null != mainBtn) {
            mainBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (activity instanceof MainActivity) {
                        return;
                    }
                    Intent intent1 = new Intent(activity, MainActivity.class);
                    startActivityForResult(intent1, 1);
                }
            });
        }
        //只有main界面才有搜索按钮
        if (activity instanceof MainActivity) {
            searchBtn = bottomView.findViewById(R.id.footer_search);
            searchBtn.setVisibility(View.VISIBLE);
            if (null != searchBtn) {
                searchBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (activity instanceof MainActivity) {
                            searchBtn.setClickable(false);
                            orderSearch();
                        }
                    }
                });
            }
        }

        if (null != myInfoBtn) {
            myInfoBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (activity instanceof MyInfoActivity) {
                        return;
                    }
                    Intent intent1 = new Intent(activity, MyInfoActivity.class);
                    startActivityForResult(intent1, 1);
                }
            });
        }
    }

activity_footer_menu

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#ffffff"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin" >

    <LinearLayout
        android:clickable="true"
        android:clipChildren="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:drawableTop="@mipmap/home"
            android:text="主页"
            android:textColor="#2c2c2c"
            android:background="@drawable/btn_white"
            android:id="@+id/footer_home"
            android:textSize="@dimen/textsize_l" />
    </LinearLayout>
    <!--搜索 -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:clickable="true"
        android:clipChildren="true"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:visibility="gone"
            android:id="@+id/footer_search"
            android:layout_width="wrap_content"
            android:background="@drawable/btn_white"
            android:layout_height="match_parent"
            android:drawableTop="@mipmap/search"
            android:text="扫描"
            android:textColor="#2c2c2c"
            android:textSize="@dimen/textsize_l" />
    </LinearLayout>

    <!--我的-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:clickable="true"
        android:clipChildren="true"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">
        <Button
            android:id="@+id/footer_my_info"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/btn_white"
            android:drawableTop="@mipmap/my"
            android:textColor="#2c2c2c"
            android:text="@string/title_my_info"
            android:textSize="@dimen/textsize_l" />
    </LinearLayout>

</LinearLayout>

个人想法:将菜单 写成一个 可拖动的远点 点击远点时展示菜单 然后点击菜单进入相关界面

猜你喜欢

转载自blog.csdn.net/guzhizang/article/details/116133918
今日推荐