android 弹出框带标题栏,Android开发靠标题栏的弹框

一、效果图

title_dialog.png

二、思路

首先它是一个弹框,只是弹框的布局做些处理,布局占满屏幕,只有需要白色的布局的背景设为白色。其他没设置背景颜色,自然用dialog的style的windowBackground

三、案例关键代码

dialog的xml

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/cl_dialog_content"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="50dp"

android:background="@color/m_white"

app:layout_constraintTop_toTopOf="parent">

android:id="@+id/iv_select_active"

android:layout_width="24dp"

android:layout_height="24dp"

android:layout_marginLeft="16dp"

android:layout_marginTop="16dp"

android:scaleType="centerCrop"

android:src="@mipmap/icon_sel_l"

android:visibility="invisible"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/tv_select_active"

style="@style/tv_w_w_s16_c_33_bold"

android:layout_width="0dp"

android:layout_height="0dp"

android:gravity="center_vertical"

android:paddingLeft="16dp"

android:text="@string/active"

app:layout_constraintBottom_toTopOf="@id/view_line"

app:layout_constraintLeft_toRightOf="@id/iv_select_active"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/view_line"

android:layout_width="0dp"

android:layout_height="1px"

android:layout_marginTop="16dp"

android:background="@color/c_f2efef"

app:layout_constraintLeft_toLeftOf="@id/iv_select_active"

app:layout_constraintTop_toBottomOf="@id/iv_select_active" />

android:id="@+id/iv_select_task"

android:layout_width="24dp"

android:layout_height="24dp"

android:layout_marginLeft="16dp"

android:layout_marginTop="16dp"

android:layout_marginBottom="16dp"

android:scaleType="centerCrop"

android:src="@mipmap/icon_sel_l"

android:visibility="invisible"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintTop_toBottomOf="@id/view_line" />

android:id="@+id/tv_select_task"

style="@style/tv_w_w_s16_c_33_bold"

android:layout_width="0dp"

android:layout_height="0dp"

android:gravity="center_vertical"

android:paddingLeft="16dp"

android:text="@string/task"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toRightOf="@id/iv_select_task"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toBottomOf="@id/view_line" />

android:id="@+id/view_empty"

android:layout_width="match_parent"

android:layout_height="0dp"

app:layout_constraintTop_toBottomOf="@id/cl_dialog_content"

app:layout_constraintBottom_toBottomOf="parent"

/>

java代码:

fun showSelectActiveOrTask(context:Context,currentSelect:String,listener: DialogClickBackListener?){

var dialog = getCancelDialog(context)

val window = dialog.window

window.setGravity(Gravity.TOP)

window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

window.setContentView(R.layout.dialog_active_task_select)

val ivSelectActive = window.findViewById(R.id.iv_select_active)

val tvSelectActive = window.findViewById(R.id.tv_select_active)

val ivSelectTask = window.findViewById(R.id.iv_select_task)

val tvSelectTask = window.findViewById(R.id.tv_select_task)

val view_empty = window.findViewById(R.id.view_empty)

if (currentSelect == "活动"){

ivSelectActive.visibility = View.VISIBLE

} else {

ivSelectTask.visibility = View.VISIBLE

}

view_empty.setOnClickListener {

listener?.onClickBack(currentSelect)

dialog.dismiss()

}

//活动

ivSelectActive.setOnClickListener {

listener?.onClickBack("活动")

dialog.dismiss()

}

tvSelectActive.setOnClickListener {

listener?.onClickBack("活动")

dialog.dismiss()

}

//任务

ivSelectTask.setOnClickListener {

listener?.onClickBack("任务")

dialog.dismiss()

}

tvSelectTask.setOnClickListener {

listener?.onClickBack("任务")

dialog.dismiss()

}

}

其中:

private fun getCancelDialog(context:Context): Dialog {

return AlertDialog.Builder(context, R.style.mydialog).setCancelable(true).show()

}

@null

true

false

true

@android:color/transparent

true

0.6

四、总结

关键是布局,dialog的style

你可能感兴趣的:(android,弹出框带标题栏)