- 此问题在此处已有答案**:
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>
1条答案
按热度按时间u91tlkcl1#
将此代码添加到MyDialog类中
在styles.xml中添加样式