[关闭]
@cxm-2016 2016-12-15T01:17:24.000000Z 字数 3677 阅读 2746

Android:DialogFragment对话框(1)

Android

版本:2
作者:陈小默
声明:禁止商业,禁止转载


1 创建一个DialogFragment的子类

以下两种实现方式二选一:如果只是想使用DialogFragment代替AlertDialog的话,采用1.2的实现方式,如果想自定义View的话就采用1.1 的实现方式

1.1 实现onCreateView()

创建要在对话框中显示的布局文件 dialog_view.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/message"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" />
  10. <Button
  11. android:id="@+id/ok"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="确定" />
  15. </LinearLayout>

重写onCreateView()方法

  1. class MyViewDialogFragment : DialogFragment() {
  2. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
  3. var view: View = inflater.inflate(R.layout.dialog_view, container, false)
  4. initView(view)
  5. return view
  6. }
  7. private fun initView(view: View) {
  8. var message = view.findViewById(R.id.message) as TextView
  9. var button = view.findViewById(R.id.ok) as Button
  10. message.text = "显示View对话框"
  11. button.setOnClickListener { view -> this.dismiss() }
  12. }
  13. }

1.1.1 设置对话框的背景透明

如果需要设置对话框的背景为透明的颜色,只需要设置

  1. dialog.window.setBackgroundDrawableResource(android.R.color.transparent)

1.2 实现onCreateDialog()

  1. class MyAlertDialogFragment : DialogFragment() {
  2. open var ctx: Activity? = null
  3. override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  4. return AlertDialog.Builder(ctx).setTitle("Title")
  5. .setMessage("Message")
  6. .setCancelable(true)
  7. .setPositiveButton("ok", DialogInterface.OnClickListener { dialogInterface, i -> this.dismiss() })
  8. .create()
  9. }
  10. }

2在Activity中显示Dialoig

这里我在Activity的布局中增加了两个Button,用来显示上述两种方法创建的对话框

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_show"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="com.hntzj.usedialogfragment.ShowActivity">
  9. <Button
  10. android:id="@+id/viewDialog"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="showViewDialog" />
  14. <Button
  15. android:id="@+id/alertDialog"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="showAlertDialog" />
  19. </LinearLayout>

showActivity.kt

  1. class ShowActivity : AppCompatActivity() {
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContentView(R.layout.activity_show)
  5. findViewById(R.id.alertDialog).setOnClickListener { view -> showAlertDialog() }
  6. findViewById(R.id.viewDialog).setOnClickListener { view -> showViewDialog() }
  7. }
  8. private fun showAlertDialog() {
  9. var dialog: MyAlertDialogFragment = MyAlertDialogFragment()
  10. dialog.ctx = this
  11. dialog.show(fragmentManager, "tag")
  12. }
  13. private fun showViewDialog() {
  14. var dialog: MyViewDialogFragment = MyViewDialogFragment()
  15. dialog.show(fragmentManager, "tag")
  16. }
  17. }

我们可以点击按钮看到效果,但是标题栏是不是很难看

3 去除对话框标题栏

对于以上创建的两种对话框,这里有不同的解决方案

3.1 AlertDialog

在创建时设置title为null

  1. override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  2. return AlertDialog.Builder(ctx).setTitle("Title")
  3. .setMessage("Message")
  4. .setCancelable(true)
  5. .setPositiveButton("ok", DialogInterface.OnClickListener { dialogInterface, i -> this.dismiss() })
  6. .setTitle("")//设置成空白字符串或者直接使用null都可以让标题栏消失
  7. .create()
  8. }

3.2 ViewDialog

设置窗体为无标题

  1. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
  2. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)//设置窗体为无标题
  3. var view: View = inflater.inflate(R.layout.dialog_view, container, false)
  4. initView(view)
  5. return view
  6. }

下一节介绍:Fragment和Activity的通信

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注