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

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

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.

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

What am I doing incorrectly?

I call the MyDialog from a fragment.

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

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

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
android:padding=&quot;10dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/tv_title&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Update&quot;
android:textAlignment=&quot;center&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;com.example.libary.forms.FormPlateState
android:id=&quot;@+id/form_plate&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginVertical=&quot;10dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/tv_title&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn_save&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginEnd=&quot;5dp&quot;
android:text=&quot;Save&quot;
android:textSize=&quot;10sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toEndOf=&quot;@id/btn_cancel&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/form_plate&quot;
/&gt;
&lt;Button
android:id=&quot;@+id/btn_cancel&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginEnd=&quot;5dp&quot;
android:text=&quot;Cancel&quot;
android:textSize=&quot;10sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintEnd_toStartOf=&quot;@id/btn_save&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/form_plate&quot;
/&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案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

&lt;style name=&quot;DialogTheme&quot; parent=&quot;Theme.AppCompat.Light.DialogWhenLarge&quot;&gt;
&lt;item name=&quot;android:windowNoTitle&quot;&gt;true&lt;/item&gt;
&lt;item name=&quot;android:windowFullscreen&quot;&gt;false&lt;/item&gt;
&lt;item name=&quot;android:windowIsFloating&quot;&gt;false&lt;/item&gt;
&lt;item name=&quot;android:statusBarColor&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
&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:

确定