【发布时间】: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