Android Popupwindow弹出窗口的另一种居中显示的方式
2019-08-19 14:17
1231 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014714188/article/details/99723320
Android Popupwindow弹出窗口的另一种居中显示的方式
网上有很多关于Popupwindow弹出框位置的文章,不管是居中显示,还是靠上,靠下等等大多数都是用的定位的方式来控制其显示位置的。本文通过另一种简便的方式来控制其居中显示。
首先,我们来看看显示的效果图
接下来就是正式开始实现
我们知道,这个弹出框其实也是一个xml布局。下面的popupwindow_service_line.xml布局代码就是这个弹出框的布局。我们从布局代码中就可以看到此时弹出框就是居中显示的。很好,这就是今天的主要内容。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#66000000" android:gravity="center" android:orientation="vertical"> <LinearLayout android:id="@+id/layout" android:layout_width="250dp" android:layout_height="wrap_content" android:background="@drawable/color_white" android:orientation="vertical"> <Button android:id="@+id/service_default" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginRight="20dp" android:gravity="center_vertical|center" android:background="@drawable/button_circle_shape" android:textColor="@color/white" android:text="@string/service_default" /> <Button android:id="@+id/service_cn" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginRight="20dp" android:gravity="center_vertical|center" android:background="@drawable/button_circle_shape" android:textColor="@color/white" android:text="@string/service_cn" /> <Button android:id="@+id/service_ind" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginRight="20dp" android:gravity="center_vertical|center" android:background="@drawable/button_circle_shape" android:textColor="@color/white" android:text="@string/service_ind" /> <Button android:id="@+id/service_sea" android:layout_width="match_parent" android:layout_height="45dp" android:layout_margin="20dp" android:gravity="center_vertical|center" android:background="@drawable/button_circle_shape" android:textColor="@color/white" android:text="@string/service_sea" /> </LinearLayout> </LinearLayout>
下面就是弹出框的调用了
我们从上面的布局文件中就看到此时弹出窗口其实就已经居中显示了,现在我们只要把这个弹出窗口的布局文件引用进来并显示出来即可,而不用再去定位计算让其居中。我们再下面的代码中可以看到有一行代码:pwindow.showAsDropDown(btn_login);这行代码本来的意思是控制其显示位置的,现在我们只要随意传入一个本Activity的控件即可。我的例子中就是传的登录的那个btn。
/** * 显示线路选择弹出框 */ private void showChoiceServiceLine() { try { View contentview = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_service_line, null); pwindow = new PopupWindow(contentview, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true); Button serviceCN = contentview.findViewById(R.id.service_cn); Button serviceIND = contentview.findViewById(R.id.service_ind); Button serviceSEA = contentview.findViewById(R.id.service_sea); Button serviceDefault = contentview.findViewById(R.id.service_default); //默认 serviceDefault.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (AppUtils.isNotFastClick()){ Util.saveServiceLine(mContext,"Default"); change_IM_line.setText(getResources().getString(R.string.service_default)); pwindow.dismiss(); } } }); //中国 serviceCN.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (AppUtils.isNotFastClick()){ Util.saveServiceLine(mContext,"CN"); change_IM_line.setText(getResources().getString(R.string.service_cn)); pwindow.dismiss(); } } }); //印度 serviceIND.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (AppUtils.isNotFastClick()){ Util.saveServiceLine(mContext,"IND"); change_IM_line.setText(getResources().getString(R.string.service_ind)); pwindow.dismiss(); } } }); //东南亚 serviceSEA.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (AppUtils.isNotFastClick()){ Util.saveServiceLine(mContext,"SEA"); change_IM_line.setText(getResources().getString(R.string.service_sea)); pwindow.dismiss(); } } }); pwindow.setFocusable(true); pwindow.setOutsideTouchable(true); pwindow.setBackgroundDrawable(new BitmapDrawable()); pwindow.showAsDropDown(btn_login); pwindow.update(); } catch (Exception e) { Log.e("showChoiceServiceLine", "Exception: " + e); } }
就这样,一个居中的弹出窗口就弄好了,是不是很简单?当然,此方法仅适用于让遮罩层遮住全部并弹出窗口居中显示的情况。如果需求是让弹出框显示在某一个btn的下面之类的,那就不能用这种方法了。当然,如果只是要弹出框在屏幕的下边啊,靠左或者靠右啊等等,这种方法很实用。我试过靠屏幕的底部的,一样的可以。
相关文章推荐
- android PopupWindow 和 Activity弹出窗口实现方式
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- JavaScript基础 window.open(url,name,options) 弹出一个 新窗口 屏幕居中显示 获取屏幕的有效宽度
- Android自定义弹出窗口PopupWindow使用技巧
- 【android】让popupwindow显示在view的上方并与该view水平居中对齐
- 在Javascript 中实现window.open弹出窗口居中显示
- 步步为营_Android开发课[26]_用户界面之PopupWindow(弹出窗口)
- JavaScript基础 window.open(url,name,options) 弹出一个 新窗口 屏幕居中显示 获取屏幕的有效宽度
- Android手机UI设计---”知乎“界面外观模仿篇(二)---在Fragment里使用自定义listview以及popupwindow弹出窗口
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- Android开发仿QQ空间根据位置弹出PopupWindow显示更多操作效果
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android PopupWindow的使用技巧 Android上自定义弹出窗口
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- 【android】让popupwindow显示在view的上方并与该view水平居中对齐
- 【Android UI设计与开发】7.底部菜单栏(四)PopupWindow 实现显示仿腾讯新闻底部弹出菜单
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- window.open弹出窗口居中显示
- Flex PopUpManager 弹出窗口在居中显示