【问题标题】:PopupWindow not shown as expected (preview is ok)PopupWindow 未按预期显示(预览正常)
【发布时间】:2021-03-18 16:44:29
【问题描述】:

我正在尝试从片段中显示全屏弹出窗口。我正在开始屏幕布局,它在 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, 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())
    }
}

最后是屏幕截图,首先是预览和预期,其次是设备上显示的内容:

【问题讨论】:

    标签: android android-layout popupwindow android-popupwindow


    【解决方案1】:
    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" 对值进行硬编码。

    【讨论】:

    • 硬编码值的结果相同。无论如何,谢谢。
    【解决方案2】:
    <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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多