英文:
DialogFragment using a custom layout width is not matching parent
问题
我显示了一个带有自定义布局文件的DialogFragment
。当对话框显示时,width
没有与父容器匹配,导致所有内容被挤压。
我是如何出错的?
我从一个fragment中调用MyDialog
。
MyDialog().show(childFragmentManager, "myDialog")
MyDialog.kt
class MyDialog : DialogFragment() {
private var _binding: DialogPlateStateBinding? = null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = DialogPlateStateBinding.inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun onResume() {
super.onResume()
binding.root.requestLayout()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.btnCancel.setOnClickListener { dismiss() }
}
}
dialog_plate_state.xml
<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="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.libary.forms.FormPlateState
android:id="@+id/form_plate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_title" />
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="Save"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/btn_cancel"
app:layout_constraintTop_toBottomOf="@id/form_plate" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="Cancel"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/btn_save"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/form_plate" />
</androidx.constraintlayout.widget.ConstraintLayout>
英文:
I am showing an DialogFragment
with a custom layout file. When the dialog is shown, the width
is not matching the parent and everything is squished.
What am I doing incorrectly?
I call the MyDialog
from a fragment.
MyDialog().show(childFragmentManager, "myDialog")
MyDialog.kt
class MyDialog : DialogFragment() {
private var _binding: DialogPlateStateBinding? = null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = DialogPlateStateBinding.inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun onResume() {
super.onResume()
binding.root.requestLayout()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.btnCancel.setOnClickListener { dismiss() }
}
}
dialog_plate_state.xml
<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="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.libary.forms.FormPlateState
android:id="@+id/form_plate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_title" />
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="Save"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/btn_cancel"
app:layout_constraintTop_toBottomOf="@id/form_plate"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="Cancel"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/btn_save"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/form_plate"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
答案1
得分: 1
将以下代码添加到 MyDialog 类中:
override fun onStart() {
super.onStart()
dialog?.window?.setDimAmount(0.1f)
dialog?.window?.setLayout(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT
)
}
override fun getTheme(): Int {
return R.style.DialogTheme
}
在 styles.xml 文件中添加样式:
<style name="DialogTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>
这些代码片段用于在 MyDialog 类中设置对话框的外观和样式,并在 styles.xml 中定义了名为 "DialogTheme" 的样式。
英文:
Add this code in MyDialog class
override fun onStart() {
super.onStart()
dialog?.window?.setDimAmount(0.1f)
dialog?.window?.setLayout(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT
)
}
override fun getTheme(): Int {
return R.style.DialogTheme
}
Add style in styles.xml
<style name="DialogTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论