问题描述
我正在尝试从片段中显示全屏弹出窗口。我正在开始屏幕布局,它在 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>
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())
}
}
最后是屏幕截图、预览和预期,然后是设备上显示的:
解决方法
- 对于加号和减号按钮,分别使用
android:margin_start="8dp"
和android:margin_end="8dp"
,以在button
和TextInputEditText
框之间留出一些空间。
立> - 如果您希望 TextInputEditText 中的零居中,请使用
android:gravity="center"
- 这些中的
@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)