您的位置:首页 > 移动开发 > Android开发

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的下面之类的,那就不能用这种方法了。当然,如果只是要弹出框在屏幕的下边啊,靠左或者靠右啊等等,这种方法很实用。我试过靠屏幕的底部的,一样的可以。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐