DialogFragment 使用自定义布局宽度不匹配父布局

huangapple go评论82阅读模式
英文:

DialogFragment using a custom layout width is not matching parent

问题

我显示了一个带有自定义布局文件的DialogFragment。当对话框显示时,width没有与父容器匹配,导致所有内容被挤压。

我是如何出错的?

我从一个fragment中调用MyDialog

  1. MyDialog().show(childFragmentManager, "myDialog")

MyDialog.kt

  1. class MyDialog : DialogFragment() {
  2. private var _binding: DialogPlateStateBinding? = null
  3. private val binding get() = _binding!!
  4. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
  5. _binding = DialogPlateStateBinding.inflate(inflater, container, false)
  6. return binding.root
  7. }
  8. override fun onDestroyView() {
  9. super.onDestroyView()
  10. _binding = null
  11. }
  12. override fun onResume() {
  13. super.onResume()
  14. binding.root.requestLayout()
  15. }
  16. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  17. binding.btnCancel.setOnClickListener { dismiss() }
  18. }
  19. }

dialog_plate_state.xml

  1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="vertical"
  7. android:padding="10dp">
  8. <TextView
  9. android:id="@+id/tv_title"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="Update"
  13. android:textAlignment="center"
  14. android:textSize="20sp"
  15. app:layout_constraintEnd_toEndOf="parent"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. <com.example.libary.forms.FormPlateState
  19. android:id="@+id/form_plate"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_marginVertical="10dp"
  23. app:layout_constraintEnd_toEndOf="parent"
  24. app:layout_constraintStart_toStartOf="parent"
  25. app:layout_constraintTop_toBottomOf="@id/tv_title" />
  26. <Button
  27. android:id="@+id/btn_save"
  28. android:layout_width="0dp"
  29. android:layout_height="wrap_content"
  30. android:layout_marginEnd="5dp"
  31. android:text="Save"
  32. android:textSize="10sp"
  33. android:textStyle="bold"
  34. app:layout_constraintEnd_toEndOf="parent"
  35. app:layout_constraintStart_toEndOf="@id/btn_cancel"
  36. app:layout_constraintTop_toBottomOf="@id/form_plate" />
  37. <Button
  38. android:id="@+id/btn_cancel"
  39. android:layout_width="0dp"
  40. android:layout_height="wrap_content"
  41. android:layout_marginEnd="5dp"
  42. android:text="Cancel"
  43. android:textSize="10sp"
  44. android:textStyle="bold"
  45. app:layout_constraintEnd_toStartOf="@id/btn_save"
  46. app:layout_constraintStart_toStartOf="parent"
  47. app:layout_constraintTop_toBottomOf="@id/form_plate" />
  48. </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.

DialogFragment 使用自定义布局宽度不匹配父布局

What am I doing incorrectly?

I call the MyDialog from a fragment.

  1. MyDialog().show(childFragmentManager, &quot;myDialog&quot;)

MyDialog.kt

  1. class MyDialog : DialogFragment() {
  2. private var _binding: DialogPlateStateBinding? = null
  3. private val binding get() = _binding!!
  4. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
  5. _binding = DialogPlateStateBinding.inflate(inflater, container, false)
  6. return binding.root
  7. }
  8. override fun onDestroyView() {
  9. super.onDestroyView()
  10. _binding = null
  11. }
  12. override fun onResume() {
  13. super.onResume()
  14. binding.root.requestLayout()
  15. }
  16. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  17. binding.btnCancel.setOnClickListener { dismiss() }
  18. }
  19. }

dialog_plate_state.xml

  1. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  2. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;wrap_content&quot;
  6. android:orientation=&quot;vertical&quot;
  7. android:padding=&quot;10dp&quot;&gt;
  8. &lt;TextView
  9. android:id=&quot;@+id/tv_title&quot;
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:text=&quot;Update&quot;
  13. android:textAlignment=&quot;center&quot;
  14. android:textSize=&quot;20sp&quot;
  15. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  16. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  17. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  18. &lt;com.example.libary.forms.FormPlateState
  19. android:id=&quot;@+id/form_plate&quot;
  20. android:layout_width=&quot;match_parent&quot;
  21. android:layout_height=&quot;wrap_content&quot;
  22. android:layout_marginVertical=&quot;10dp&quot;
  23. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  24. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  25. app:layout_constraintTop_toBottomOf=&quot;@id/tv_title&quot; /&gt;
  26. &lt;Button
  27. android:id=&quot;@+id/btn_save&quot;
  28. android:layout_width=&quot;0dp&quot;
  29. android:layout_height=&quot;wrap_content&quot;
  30. android:layout_marginEnd=&quot;5dp&quot;
  31. android:text=&quot;Save&quot;
  32. android:textSize=&quot;10sp&quot;
  33. android:textStyle=&quot;bold&quot;
  34. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  35. app:layout_constraintStart_toEndOf=&quot;@id/btn_cancel&quot;
  36. app:layout_constraintTop_toBottomOf=&quot;@id/form_plate&quot;
  37. /&gt;
  38. &lt;Button
  39. android:id=&quot;@+id/btn_cancel&quot;
  40. android:layout_width=&quot;0dp&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:layout_marginEnd=&quot;5dp&quot;
  43. android:text=&quot;Cancel&quot;
  44. android:textSize=&quot;10sp&quot;
  45. android:textStyle=&quot;bold&quot;
  46. app:layout_constraintEnd_toStartOf=&quot;@id/btn_save&quot;
  47. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  48. app:layout_constraintTop_toBottomOf=&quot;@id/form_plate&quot;
  49. /&gt;
  50. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 1

将以下代码添加到 MyDialog 类中:

  1. override fun onStart() {
  2. super.onStart()
  3. dialog?.window?.setDimAmount(0.1f)
  4. dialog?.window?.setLayout(
  5. WindowManager.LayoutParams.MATCH_PARENT,
  6. WindowManager.LayoutParams.MATCH_PARENT
  7. )
  8. }
  9. override fun getTheme(): Int {
  10. return R.style.DialogTheme
  11. }

在 styles.xml 文件中添加样式:

  1. <style name="DialogTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
  2. <item name="android:windowNoTitle">true</item>
  3. <item name="android:windowFullscreen">false</item>
  4. <item name="android:windowIsFloating">false</item>
  5. <item name="android:statusBarColor">@color/colorPrimaryDark</item>
  6. </style>

这些代码片段用于在 MyDialog 类中设置对话框的外观和样式,并在 styles.xml 中定义了名为 "DialogTheme" 的样式。

英文:

Add this code in MyDialog class

  1. override fun onStart() {
  2. super.onStart()
  3. dialog?.window?.setDimAmount(0.1f)
  4. dialog?.window?.setLayout(
  5. WindowManager.LayoutParams.MATCH_PARENT,
  6. WindowManager.LayoutParams.MATCH_PARENT
  7. )
  8. }
  9. override fun getTheme(): Int {
  10. return R.style.DialogTheme
  11. }

Add style in styles.xml

  1. &lt;style name=&quot;DialogTheme&quot; parent=&quot;Theme.AppCompat.Light.DialogWhenLarge&quot;&gt;
  2. &lt;item name=&quot;android:windowNoTitle&quot;&gt;true&lt;/item&gt;
  3. &lt;item name=&quot;android:windowFullscreen&quot;&gt;false&lt;/item&gt;
  4. &lt;item name=&quot;android:windowIsFloating&quot;&gt;false&lt;/item&gt;
  5. &lt;item name=&quot;android:statusBarColor&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
  6. &lt;/style&gt;

huangapple
  • 本文由 发表于 2023年2月8日 21:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定