经与弹出窗口问题

问题描述 投票:7回答:4

我想设置类似老Facebook的注释部分弹出窗口。

圆角对话框,但我面临的对话框size of dialog boxshowatlocation问题。

当我尝试不同的手机验证码

        val display = windowManager.defaultDisplay
        val size = Point()
        display.getSize(size)
        val popupWindow = PopupWindow(customView, size.x-30, size.y-300, true)
        popupWindow.showAtLocation(linearLayout1, Gravity.CENTER, -3, 100)
        popupWindow.setAnimationStyle(R.style.PopupAnimation)

XML文件:

<?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="wrap_content"
    android:background="@drawable/dialog_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10px">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/popup_rcv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/new_comment_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Please enter Comment" />

        <Button
            android:id="@+id/comment_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit" />
    </LinearLayout>
</LinearLayout>

Source //动画

输出上万普拉斯6T:One plus

在一个加我的动画也禁用当我改变.showAtLocation到其它号码

联想K8注输出:

K8 Note

在弹出的变化K8注位置,如果我改变为另一个值。

先感谢您。

android kotlin popupwindow
4个回答
5
投票

貌似这两个手机有不同的屏幕分辨率。所以,你必须使用而不是像素DP。勾选此post

弹出的大小。首先,你必须对话框的大小转换为像素。这些都是随机值,它们是硬编码的,但你可以把他们从资源或harcode过。

val popUpWidthDp = 200
val popUpHeightDp = 100

val popUpWidthPx = convertDpToPx(popUpWidthDp)
val popUpHeightPx = convertDpToPx(popUpHeightDp)

val popupWindow = PopupWindow(customView, popUpWidthPx, popUpHeightPx, true)

弹出位置。首先,你需要DP到PX转换,然后你可以计算出相关的屏幕尺寸弹出位置。

val popUpLeftSideMarginDp = 50
val popUpTopMarginDp = 100

val popUpXPoint = convertDpToPx(popUpLeftSideMarginDp)
val popUpYPoint = convertDpToPx(popUpTopMarginDp)

popupWindow.showAtLocation(linearLayout1, Gravity.CENTER, popUpXPoint, popUpYPoint)

看看这个answer了解如何DP转换为像素,反之亦然。

如果弹出窗口应具有与屏幕尺寸的大小和位置,那么你必须要改变这些值:

  • popUpHeightPx,popUpWidthPx - 弹出尺寸
  • popUpXPoint,popUpYPoint - 弹出位置

让我知道如果你需要详细的解释。


1
投票

创建PopupWindow为:

popupWindow= new PopupWindow(
                        customView,
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT
                );

并设置其位置为:

popupWindow.showAtLocation(linearLayout1, Gravity.CENTER, 0, 0)

并添加customView上边距10dp或5DP在XML


0
投票

试试下面的解决方案:它在Java中,但你可以将其转换在科特林

PopupWindow popupWin = new PopupWindow(mContext);

// Measure layout here, then we can get measureHeight.
layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWin .setWidth(layout.getMeasuredWidth()); 
popupWin .setHeight(layout.getMeasuredHeight()); //you can pass height as you want.
popupWin .setContentView(layout);
popupWin .showAsDropDown(anchorView,0 ,0, Gravity.NO_GRAVITY);

这里anchorView是在我们要在打开PopupWindow的下拉列表中选择视图。

PopupWindow


0
投票

我想你想的像素转换成DP,而不是DP集成到像素,以适应屏幕的不同分辨率。更多信息here

这是how像素转换为DP。

© www.soinside.com 2019 - 2024. All rights reserved.