首页 >> 大全

PopupWindow的简单使用(结合RecyclerView)

2023-08-03 大全 22 作者:考证青年

弹窗

在中弹出式菜单(以下称弹窗)是使用十分广泛一种菜单呈现的方式,弹窗为用户交互提供了便利。关于弹窗的实现大致有以下两种方式和;

两者的区别:弹窗在位置显示上是固定的,而则相对比较随意,能够在主屏幕上的任意位置显示;

今天就简单介绍一下,如何利用实现的自定义的弹窗布局

使用步骤:

1.创建两个xml文件,一个主布局,一个是布局(因为我是在项目里写的,所以闲杂代码可能比较多):

主布局(在其写一个按钮,因为项目需要,我换成了):

<LinearLayout
        android:id="@+id/score_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"  app:layout_behavior="@string/appbar_scrolling_view_behavior"android:background="#f0f0f0"><ImageView
                android:id="@+id/bttest"android:layout_width="50dp"android:layout_height="match_parent"android:layout_gravity="center"android:src="@mipmap/selectteam" />LinearLayout>

popupwindow使用__水泵结合器的使用

.xml布局(里面放一个):

"http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="vertical">.support.v7.widget.RecyclerViewandroid:id="@+id/select"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_scrollFlags="scroll|enterAlways|snap">.support.v7.widget.RecyclerView>

2.在中为进行实例化,并为其设立点击事件:

    bselect.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {showPopupWindow();}});

private void showPopupWindow() {View view = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow,null);RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.select);LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);recyclerView.setLayoutManager(linearLayoutManager);ScoreTeamAdapter scoreTeamAdapter = new ScoreTeamAdapter(yearList);recyclerView.setAdapter(scoreTeamAdapter);popupWindow = new PopupWindow(main_layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);popupWindow.setContentView(view);popupWindow.setFocusable(true);popupWindow.showAsDropDown(bselect);}

()方法:

1.先将.xml布局加载成一个View,并通过该View将进行实例化,然后进行设置,在这设置成竖向排列的线性布局,然后为其设置一个;

2.随后将进行设置:

水泵结合器的使用__popupwindow使用

popupWindow = new PopupWindow(main_layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

是的父容器,

第一个..是设置的宽度,第二个..是设置的高度;

popupWindow.setContentView(view);

设置的布局;

popupWindow.showAsDropDown(bselect);

调用的(View view)将作为View组件的下拉组件显示出来;或调用的()方法将在指定位置显示出来;

最后的效果如图:

这里写图片描述

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了