PopupWindow 未按预期显示预览正常

如何解决PopupWindow 未按预期显示预览正常

我正在尝试从片段中显示全屏弹出窗口。我正在开始屏幕布局,它在 AS 屏幕预览中看起来像预期的那样,但在设备中执行时却没有。基本上,似乎省略了填充。所有这些,布局根部的填充,在我用来增加可点击区域的图像视图中,在文本输入布局中(由主题添加)。 另一方面,动画在某些设备中不显示,但在模拟器中显示。 这是布局文件``:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_gray"
    android:fitsSystemWindows="true"
    android:paddingStart="@dimen/padding_start"
    android:paddingTop="@dimen/padding_top"
    android:paddingEnd="@dimen/padding_end"
    android:paddingBottom="@dimen/padding_bottom">

    <ImageView
        android:id="@+id/bt_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/grid_size_x2_5"
        android:src="@drawable/ic_close"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_title"
        style="@style/TextAppearance.Touche.H3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Product"
        android:text="Product"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/bt_close"
        tools:textColorHint="@color/white" />

    <Button
        android:id="@+id/bt_add_item"
        style="@style/TextAppearance.Touche.Button.Primary"
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height"
        android:text="@string/add_order_add__to_order"
        app:layout_constraintBottom_toBottomOf="parent" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/ti_quantity"
        android:layout_width="@dimen/grid_size_x10"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/grid_size_x6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bt_close">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_quantity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:lines="1"
            android:maxLength="50"
            android:paddingStart="@dimen/padding_start"
            android:paddingTop="@dimen/padding_top"
            android:paddingEnd="@dimen/padding_end"
            android:paddingBottom="@dimen/padding_bottom"
            android:text="0" />
    </com.google.android.material.textfield.TextInputLayout>

    <ImageButton
        android:id="@+id/bt_less"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:paddingStart="@dimen/padding_start"
        android:paddingTop="@dimen/padding_top"
        android:paddingEnd="@dimen/padding_end"
        android:paddingBottom="@dimen/padding_bottom"
        android:src="@drawable/ic_less_selector"
        app:layout_constraintBottom_toBottomOf="@id/ti_quantity"
        app:layout_constraintEnd_toStartOf="@id/ti_quantity"
        app:layout_constraintTop_toTopOf="@id/ti_quantity" />

    <ImageButton
        android:id="@+id/bt_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:paddingStart="@dimen/padding_start"
        android:paddingTop="@dimen/padding_top"
        android:paddingEnd="@dimen/padding_end"
        android:paddingBottom="@dimen/padding_bottom"
        android:src="@drawable/ic_add_selector"
        app:layout_constraintBottom_toBottomOf="@id/ti_quantity"
        app:layout_constraintStart_toEndOf="@id/ti_quantity"
        app:layout_constraintTop_toTopOf="@id/ti_quantity" />

</androidx.constraintlayout.widget.ConstraintLayout>

这里有一个片段中的 kotlin 代码

context?.let {
    val customView: View = (it.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.popup_add_item,null)
    val popupWindow = PopupWindow(it)
    popupWindow.contentView = customView
    popupWindow.width = LinearLayout.LayoutParams.MATCH_PARENT
    popupWindow.height = LinearLayout.LayoutParams.MATCH_PARENT
    popupWindow.animationStyle = R.style.popup_window_animation // Todo Not working in some devices
    popupWindow.elevation = 5f
    popupWindow.showAtLocation(getBinding().rvdiscounts,Gravity.CENTER,0)
    val btClose = customView.findViewById<ImageView>(R.id.bt_close)
    val btAddToCart = customView.findViewById<Button>(R.id.bt_add_item)
    val btAdd = customView.findViewById<ImageButton>(R.id.bt_add)
    val btLess = customView.findViewById<ImageButton>(R.id.bt_less)
    val etQuantity = customView.findViewById<TextInputEditText>(R.id.et_quantity)
    btClose.setonClickListener {
        popupWindow.dismiss()
    }
    btAddToCart.setonClickListener {
        // Todo add to cart
        popupWindow.dismiss()
    }
    btAdd.setonClickListener {
        val currentQuantity = etQuantity.text.toString().toInt()
        etQuantity.setText(currentQuantity.inc().toString())
    }
    btLess.setonClickListener {
        val currentQuantity = etQuantity.text.toString().toInt()
        if (currentQuantity > 0) etQuantity.setText(currentQuantity.dec().toString())
    }
}

最后是屏幕截图、预览和预期,然后是设备上显示的:

enter image description here

enter image description here

解决方法

  1. 对于加号和减号按钮,分别使用 android:margin_start="8dp"android:margin_end="8dp",以在 buttonTextInputEditText 框之间留出一些空间。
    立>
  2. 如果您希望 TextInputEditText 中的零居中,请使用 android:gravity="center"
  3. 这些中的 @dimen/padding_start 值可能不可接受,因此只需使用 android:paddingHorizontal="8dp"android:paddingVertical="8dp" 对值进行硬编码。
,
<style name="AppTheme.FullScreenDialog" parent="Theme.MaterialComponents.Light.Dialog">
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="actionMenuTextColor">@color/colorAccent</item>
</style>

在片段中创建对话框时使用此样式,如下所示:

 var fullScreenCameraDialog = Dialog(context,R.style.AppTheme_FullScreenDialog)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?