@cxm-2016
2016-12-15T09:17:24.000000Z
字数 3677
阅读 2857
Android
版本:2
作者:陈小默
声明:禁止商业,禁止转载
以下两种实现方式二选一:如果只是想使用DialogFragment代替AlertDialog的话,采用1.2的实现方式,如果想自定义View的话就采用1.1 的实现方式
创建要在对话框中显示的布局文件 dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<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() }
}
}
如果需要设置对话框的背景为透明的颜色,只需要设置
dialog.window.setBackgroundDrawableResource(android.R.color.transparent)
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,用来显示上述两种方法创建的对话框
<?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: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的通信