android - 自定义主题未实现到 DialogFragment
问题描述
我在DialogFragment
这里创建了一个扩展:
class AlertDialogFragment(context: Context) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = AlertDialog.Builder(activity).apply {
setStyle(STYLE_NO_FRAME, R.style.AlertDialogTheme)
setNeutralButton("Cancel", object : DialogInterface.OnClickListener{
override fun onClick(dialog: DialogInterface?, which: Int) {
}
})
setPositiveButton("Replace", object : DialogInterface.OnClickListener{
override fun onClick(dialog: DialogInterface?, which: Int) {
}
})
setNegativeButton("Delete", object : DialogInterface.OnClickListener{
override fun onClick(dialog: DialogInterface?, which: Int) {
}
})
}
return dialog.create()
}
}
正如您在上面看到的,我已将 my 应用于AlertDialogTheme
对话框:
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/colorPrimary</item>
<item name="android:windowBackground">@color/colorPrimary</item>
</style>
但是,当显示我的对话框时:
override fun onClick(v: View) {
if (v is AppCompatImageButton){
val dialog = AlertDialogFragment(this)
dialog.show(supportFragmentManager, "alertDialog")
}
}
对话框的背景是黑色的(我认为是~#333)。我@color/colorPrimary
是白色的,所以这就是背景的意思。
知道问题是什么吗?
解决方案
你可以那样做
class AlertDialogFragment : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container:ViewGroup?,savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_dialog, container, false)
view.delete.setOnClickListener { }
view.cancel.setOnClickListener { }
view.replace.setOnClickListener { }
return view
}
}
创建 xml 布局fragment_dialog并根据需要进行设计:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="cancel"
android:layout_weight="1"/>
<Button
android:id="@+id/replace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="replace"
android:layout_weight="1"/>
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="delete"
android:layout_weight="1"/>
</LinearLayout>
最后以您的自定义样式显示您的对话框,就像这样:
val dialog = AlertDialogFragment()
dialog.setStyle(DialogFragment.STYLE_NO_FRAME , R.style.AlertDialogTheme)
dialog.show(supportFragmentManager, "alertDialog")
推荐阅读
- go - 不同的结构重用相同的方法
- node.js - Node JS 注销功能显示错误:`onWebTokenError: jwt malformed`
- locking - 当我们向其中添加列时,HANA 会锁定整个表吗?
- c# - 如何使用 c# 在 azure 表存储中查找 2 dateTime 之间的小时数
- java - 从具有高效时间复杂度的文件中搜索记录。文件有 10 亿条记录。带键值对
- spring-boot - Spring Boot Security - 证书 - 如果无效则拒绝连接
- c# - 如何在 C# 中读取 SSIS 包组件内部的绑定
- excel - 您可以使用逻辑应用将共享点上的 excel 文件中的数据插入 sql azure db 吗?
- azure - Azure 机器学习中的终结点不可使用
- plotly - 突出来源来源的桑基图