如何自定义一个 DialogFragment?
How to customize a DialogFragment?
我尝试创建自定义 DialogFragment。问题是它看起来不像我想要的那样。
我创建了一个布局:
<?xml version="1.0" encoding="utf-8"?>
<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="239dp"
android:background="#00f">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/pop_up_shape"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
它看起来像这样:
我创建了一个 class:
class AddPlaylistPopUp: DialogFragment(){
override fun onCreateDialog(
savedInstanceState: Bundle?
): Dialog {
val builder = AlertDialog.Builder(context!!)
builder.setView(R.layout.add_playlist_popup_layout)
return builder.create()
}
}
我是这样呈现的:
fun openDialog(){
val fm = fragmentManager!!
val editNameDialogFragment = AddPlaylistPopUp()
editNameDialogFragment.show(fm, "fragment_edit_name")
}
这是结果:
如您所见,弹出窗口看起来不像我创建的自定义布局...
我究竟做错了什么?
有不必要的侧边距,绿色视图更大,按钮在绿色视图内。我尝试了很多东西,但没有任何效果。如何让它看起来 100% 像我的布局?
我找到了解决办法。可以使用 Dialog 代替 DialogFragment。
class AddPlaylistPopUp(context: Context, themeResId: Int) : Dialog(context, themeResId) {
init {
window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.add_playlist_popup_layout)
popUpContainer.setOnClickListener { dismiss() }
popUpCard.setOnClickListener {}
}
}
可以这样使用:
private fun openDialog(){
val addPlaylistPopUp = AddPlaylistPopUp(this.context!!, R.style.DialogAlertTheme)
addPlaylistPopUp.show()
}
这是风格:
<!-- Dialog theme -->
<style name="DialogAlertTheme">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
我也稍微改变了一下布局:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:id="@+id/popUpContainer"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="239dp"
android:background="#00f"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/popUpBackground"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/popUpCard"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/pop_up_shape"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:focusable="false"
android:clickable="false"
>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
结果如我所愿:
我尝试创建自定义 DialogFragment。问题是它看起来不像我想要的那样。 我创建了一个布局:
<?xml version="1.0" encoding="utf-8"?>
<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="239dp"
android:background="#00f">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/pop_up_shape"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
它看起来像这样:
我创建了一个 class:
class AddPlaylistPopUp: DialogFragment(){
override fun onCreateDialog(
savedInstanceState: Bundle?
): Dialog {
val builder = AlertDialog.Builder(context!!)
builder.setView(R.layout.add_playlist_popup_layout)
return builder.create()
}
}
我是这样呈现的:
fun openDialog(){
val fm = fragmentManager!!
val editNameDialogFragment = AddPlaylistPopUp()
editNameDialogFragment.show(fm, "fragment_edit_name")
}
这是结果:
如您所见,弹出窗口看起来不像我创建的自定义布局... 我究竟做错了什么? 有不必要的侧边距,绿色视图更大,按钮在绿色视图内。我尝试了很多东西,但没有任何效果。如何让它看起来 100% 像我的布局?
我找到了解决办法。可以使用 Dialog 代替 DialogFragment。
class AddPlaylistPopUp(context: Context, themeResId: Int) : Dialog(context, themeResId) {
init {
window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.add_playlist_popup_layout)
popUpContainer.setOnClickListener { dismiss() }
popUpCard.setOnClickListener {}
}
}
可以这样使用:
private fun openDialog(){
val addPlaylistPopUp = AddPlaylistPopUp(this.context!!, R.style.DialogAlertTheme)
addPlaylistPopUp.show()
}
这是风格:
<!-- Dialog theme -->
<style name="DialogAlertTheme">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
我也稍微改变了一下布局:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:id="@+id/popUpContainer"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="239dp"
android:background="#00f"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/popUpBackground"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/popUpCard"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/pop_up_shape"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:focusable="false"
android:clickable="false"
>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
结果如我所愿: