美文网首页
Android开发靠标题栏的弹框

Android开发靠标题栏的弹框

作者: 你的益达233 | 来源:发表于2021-01-11 09:46 被阅读0次

    一、效果图

    title_dialog.png

    二、思路

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

    三、案例关键代码

    dialog的xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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.support.constraint.ConstraintLayout
        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">
    
        <ImageView
            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" />
    
        <TextView
            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" />
    
        <View
            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" />
    
        <ImageView
            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" />
    
        <TextView
            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.support.constraint.ConstraintLayout>
    
    <View
        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"
        />
    </android.support.constraint.ConstraintLayout>
    

    java代码:

    fun showSelectActiveOrTask(context:Context,currentSelect:String,listener: DialogClickBackListener<String>?){
    
        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<ImageView>(R.id.iv_select_active)
        val tvSelectActive = window.findViewById<TextView>(R.id.tv_select_active)
        val ivSelectTask = window.findViewById<ImageView>(R.id.iv_select_task)
        val tvSelectTask = window.findViewById<TextView>(R.id.tv_select_task)
        val view_empty = window.findViewById<View>(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()
    }
    
    <style name="mydialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item><!--边框-->
        <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
        <item name="android:windowIsTranslucent">false</item><!--半透明-->
        <item name="android:windowNoTitle">true</item><!--无标题-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item><!--模糊-->
        <item name="android:backgroundDimAmount">0.6</item>
    </style>
    

    四、总结

    关键是布局,dialog的style

    相关文章

      网友评论

          本文标题:Android开发靠标题栏的弹框

          本文链接:https://www.haomeiwen.com/subject/ltiraktx.html