标签(空格分隔): android fragment dialog
作者:陈小默
注:以下使用Kotlin语言实现
以下两种实现方式二选一:如果只是想使用DialogFragment代替AlertDialog的话,采用1.2的实现方式,如果想自定义View的话就采用1.1 的实现方式
创建要在对话框中显示的布局文件 dialog_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
LinearLayout>
重写onCreateView()方法
class MyViewDialogFragment : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
var view: View = inflater.inflate(R.layout.dialog_view, container, false)
initView(view)
return view
}
private fun initView(view: View) {
var message = view.findViewById(R.id.message) as TextView
var button = view.findViewById(R.id.ok) as Button
message.text = "显示View对话框"
button.setOnClickListener { view -> this.dismiss() }
}
}
class MyAlertDialogFragment : DialogFragment() {
open var ctx: Activity? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(ctx).setTitle("Title")
.setMessage("Message")
.setCancelable(true)
.setPositiveButton("ok", DialogInterface.OnClickListener { dialogInterface, i -> this.dismiss() })
.create()
}
}
这里我在Activity的布局中增加了两个Button,用来显示上述两种方法创建的对话框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hntzj.usedialogfragment.ShowActivity">
<Button
android:id="@+id/viewDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showViewDialog" />
<Button
android:id="@+id/alertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showAlertDialog" />
LinearLayout>
showActivity.kt
class ShowActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_show)
findViewById(R.id.alertDialog).setOnClickListener { view -> showAlertDialog() }
findViewById(R.id.viewDialog).setOnClickListener { view -> showViewDialog() }
}
private fun showAlertDialog() {
var dialog: MyAlertDialogFragment = MyAlertDialogFragment()
dialog.ctx = this
dialog.show(fragmentManager, "tag")
}
private fun showViewDialog() {
var dialog: MyViewDialogFragment = MyViewDialogFragment()
dialog.show(fragmentManager, "tag")
}
}
我们可以点击按钮看到效果,但是标题栏是不是很难看
对于以上创建的两种对话框,这里有不同的解决方案
在创建时设置title为null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AlertDialog.Builder(ctx).setTitle("Title")
.setMessage("Message")
.setCancelable(true)
.setPositiveButton("ok", DialogInterface.OnClickListener { dialogInterface, i -> this.dismiss() })
.setTitle("")//设置成空白字符串或者直接使用null都可以让标题栏消失
.create()
}
设置窗体为无标题
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)//设置窗体为无标题
var view: View = inflater.inflate(R.layout.dialog_view, container, false)
initView(view)
return view
}
下一节介绍:Fragment和Activity的通信