kotlin 使用自定义布局宽度的DialogFragment与父级不匹配[重复]

ljsrvy3e  于 2023-02-09  发布在  Kotlin
关注(0)|答案(1)|浏览(144)
    • 此问题在此处已有答案**:

How to make dialogFragment width match parent?(5个答案)
How do I display a full-width DialogFragment?(5个答案)
How to make DialogFragment width to Fill_Parent(20个答案)
15小时前关门了。
我正在显示一个带有自定义布局文件的DialogFragment。当对话框显示时,width与父文件不匹配,所有内容都被压扁了。

我做错了什么?
我从一个 * fragment * 调用MyDialog

MyDialog().show(childFragmentManager, "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>
u91tlkcl

u91tlkcl1#

将此代码添加到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>

相关问题