1. 程式人生 > >kotlin實現Android實現底部對話方塊BottomDialog(圓角,直角)

kotlin實現Android實現底部對話方塊BottomDialog(圓角,直角)

本文借鑑秀原始碼:http://www.xiufm.com/thread-15571-1-3.html

圖片借鑑秀原始碼gif


先來看main.java

class Main3Activity : AppCompatActivity() {

    private fun show2() {
        var button1Dialog: Dialog = Dialog(this,R.style.ButtomDialog)
        var view: View = LayoutInflater.from(this).inflate(R.layout.dialog_content_circle,null)
        button1Dialog.setContentView(view)
        var layoutParams: ViewGroup.MarginLayoutParams = view.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.width = resources.displayMetrics.widthPixels - DensityUtil.dp2px(this,16f)
        layoutParams.bottomMargin = DensityUtil.dp2px(this,8f)
        view.layoutParams = layoutParams
        button1Dialog.setCanceledOnTouchOutside(true)
        button1Dialog.window.setGravity(Gravity.BOTTOM)
        button1Dialog.window.setWindowAnimations(R.style.BottomDialog_Animation)
        button1Dialog.show()
    }

    private fun show1() {
        var button1Dialog: Dialog = Dialog(this,R.style.ButtomDialog)
        var view: View = LayoutInflater.from(this).inflate(R.layout.dialog_content_normal,null)
        button1Dialog.setContentView(view)
        var layoutParams: ViewGroup.LayoutParams = view.layoutParams
        layoutParams.width = resources.displayMetrics.widthPixels
        view.layoutParams = layoutParams
        button1Dialog.window.setGravity(Gravity.BOTTOM)
        button1Dialog.window.setWindowAnimations(R.style.BottomDialog_Animation)
        button1Dialog.show()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main3)
        initview()
    }

    private fun initview() {
        btn_show_1.setOnClickListener{show1()}
        btn_show_2.setOnClickListener{show2()}
    }
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.text.paintclockapplication.Activity.Main3Activity">

    <Button
        android:id="@+id/btn_show_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="彈出底部對話方塊"/>

    <Button
        android:id="@+id/btn_show_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="彈出底部圓角對話方塊"/>
</LinearLayout>
封裝的畫素類,聽不錯的:
object  DensityUtil {
    /**
     * dp轉px
     *
     * @param context
     * @param dpVal
     * @return
     */
    fun dp2px(context: Context, dpFloat: Float): Int{
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpFloat,context.resources.displayMetrics).toInt()
    }
    /**
     * sp轉px
     *
     * @param context
     * @param spVal
     * @return
     */
    fun sp2px(context: Context,spFloat: Float): Int{
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spFloat,context.resources.displayMetrics).toInt()
    }
    /**
     * px轉dp
     *
     * @param context
     * @param pxVal
     * @return
     */
    fun px2dp(context: Context,pxFloat: Float): Float{
        var scale : Float = context.resources.displayMetrics.density
        return (pxFloat/scale)
    }
    /**
     * px轉sp
     *
     * @param pxVal
     * @param pxVal
     * @return
     */
    fun px2sp(context: Context,pxFloat: Float):Float{
        return (pxFloat/context.resources.displayMetrics.scaledDensity)
    }
}
dialog_content_circle.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="wrap_content"
              android:background="@drawable/shape_dialog"
              android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_edit"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="設定備註及標籤"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_star"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="標為星標朋友"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_circle"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="設定朋友圈許可權"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_send"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="傳送該名片"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_alert"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="投訴"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_delete"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="刪除"
        android:textColor="#666666"
        android:textSize="14sp"/>
</LinearLayout>
dialog_content_normal.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="wrap_content"
              android:background="@android:color/white"
              android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_edit"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="設定備註及標籤"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_star"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="標為星標朋友"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_circle"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="設定朋友圈許可權"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_send"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="傳送該名片"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_alert"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="投訴"
        android:textColor="#666666"
        android:textSize="14sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_delete"
        android:drawablePadding="16dp"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:text="刪除"
        android:textColor="#666666"
        android:textSize="14sp"/>
</LinearLayout>
這些都放在drawlable檔案裡面
ic_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:viewportWidth="1024"
        android:viewportHeight="1024"
        android:width="18dp"
        android:height="18dp">
    <path android:fillColor="#FF666666"
          android:pathData="M1006.4 876.4 585.6 64.128c-40.464-78.128-106.72-78.128-147.184 0L17.6 876.4c-40.464 78.128-1.6 142.064 86.4 142.064l816 0C1008 1018.464 1046.88 954.528 1006.4 876.4zM460.816 313.168c0-28.256 22.912-51.184 51.184-51.184s51.216 22.944 51.216 51.184l0 345.632c0 28.256-22.944 51.184-51.216 51.184s-51.184-22.944-51.184-51.184L460.816 313.168zM512 905.168c-35.344 0-64-28.624-64-64 0-35.344 28.656-64 64-64s64 28.656 64 64C576 876.544 547.344 905.168 512 905.168z"/>
</vector>

ic_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="18dp"
        android:height="18dp"
        android:viewportHeight="1024"
        android:viewportWidth="1024">
    <path
        android:fillColor="#FF666666"
        android:pathData="M358.232894 664.748916c-0.067538 0.001023-0.135076 0.001023-0.202615 0.002047-4.583388-0.028653-8.996908-1.850138-12.238742-5.104251L231.670383 544.742728c-6.722098-6.77531-6.698562-17.73492 0.076748-24.490787L683.96763 69.079308c3.25309-3.241834 7.996115-4.39817 12.269441-5.043876 4.583388 0.028653 8.996908 1.850138 12.238742 5.104251L822.578548 184.009897c6.722098 6.77531 6.698562 17.73492-0.076748 24.490787L370.299721 659.707087C367.096772 662.914128 362.765117 664.710031 358.232894 664.748916z"/>
    <path
        android:fillColor="#FF666666"
        android:pathData="M132.691091 770.20062c-4.532223 0-8.946766-1.792832-12.227486-5.073552-4.194532-4.160763-5.969968-10.214642-4.684696-15.999392l32.945343-148.389632c1.370207-6.15621 5.987365-11.094686 12.05864-12.853749s12.616342-0.101307 17.081027 4.329609L293.375747 707.320501c4.482081 4.464685 6.172583 11.027148 4.414543 17.08205-1.759063 6.088672-6.680143 10.722202-12.853749 12.108782L136.479371 769.795391C135.227868 770.065544 133.958968 770.20062 132.691091 770.20062z"/>
    <path
        android:fillColor="#FF666666"
        android:pathData="M874.130667 958.814372 150.009526 958.814372c-28.700669 0-51.955302-23.271006-51.955302-51.955302 0-28.683273 23.254633-51.955302 51.955302-51.955302l724.121142 0c28.700669 0 51.955302 23.271006 51.955302 51.955302C926.08597 935.543366 902.831336 958.814372 874.130667 958.814372z"/>
</vector>

ic_star.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="18dp"
        android:height="18dp"
        android:viewportHeight="512"
        android:viewportWidth="512">
    <path
        android:fillColor="#ff666666"
        android:pathData="M256 30.72l61.133 172.083h164.147l-133.939 101.069 47.872 177.408-139.213-106.342-139.213 106.342 47.872-177.408-133.939-101.069h164.147z"/>
</vector>

ic_cricle

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:viewportWidth="1024"
        android:viewportHeight="1024"
        android:width="18dp"
        android:height="18dp">
    <path android:fillColor="#FF666666"
          android:pathData="M348.83584 644.01408 83.70176 378.896384c-12.787712 41.644032-19.683328 85.87264-19.683328 131.724288 0 50.643968 8.408064 99.311616 23.880704 144.703488l270.917632 0C355.388416 651.657216 352.0256 647.901184 348.83584 644.01408z"/>
    <path android:fillColor="#FF666666"
          android:pathData="M936.77056 365.924352 665.853952 365.924352c3.453952 3.657728 6.790144 7.445504 9.980928 11.331584l265.109504 265.115648c12.795904-41.66144 19.689472-85.87264 19.689472-131.750912C960.632832 459.983872 952.246272 411.31008 936.77056 365.924352z"/>
    <path android:fillColor="#FF666666"
          android:pathData="M907.916288 299.512832c-44.222464-82.597888-113.456128-149.74464-197.69856-191.222784L518.995968 299.512832 907.916288 299.512832z"/>
    <path android:fillColor="#FF666666"
          android:pathData="M116.791296 721.74592c43.990016 82.269184 112.868352 149.177344 196.64384 190.73024l190.710784-190.73024L116.791296 721.74592z"/>
    <path android:fillColor="#FF666666"
          android:pathData="M722.868224 525.689856l0 380.836864c82.536448-44.009472 149.6832-113.043456 191.307776-196.989952L723.243008 518.614016C723.160064 520.987648 723.014656 523.36128 722.868224 525.689856z"/>
    <path android:fillColor="#FF666666"
          android:pathData="M512.331776 62.319616c-50.64704 0-99.311616 8.404992-144.69632 23.860224l0 270.7712L642.92864 81.67936C601.614336 69.085184 557.761536 62.319616 512.331776 62.319616z"/>
    <path android:fillColor="#FF666666"
          android:pathData="M380.607488 939.232256c41.634816 12.807168 85.87264 19.698688 131.724288 19.698688 50.435072 0 98.870272-8.302592 144.108544-23.64928L656.44032 664.701952c-3.467264 3.254272-7.042048 6.416384-10.719232 9.43104L380.607488 939.232256z"/>
    <path android:fillColor="#FF666666"
          android:pathData="M301.232128 115.063808c-82.2784 44.018688-149.21216 112.886784-190.729216 196.661248l190.729216 190.72L301.232128 115.063808z"/>
</vector>

ic_send.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:viewportWidth="1024"
        android:viewportHeight="1024"
        android:width="18dp"
        android:height="18dp">
    <path android:fillColor="#FF666666"
          android:pathData="M668.443563 462.989773v148.793838l261.966387-217.993896-261.966387-209.530131v131.190925c-337.916173 0.040932-354.177532 334.058308-354.177531 334.058308 95.647407-157.093874 154.497747-186.519044 337.919242-186.519044h16.258289z"/>
    <path android:fillColor="#FF666666"
          android:pathData="M720.839911 701.264575v32.072462H196.90509V366.588189h87.810929s28.880771-58.512648 90.954524-104.786555H118.315174c-14.467503 0-26.196639 11.729136-26.196638 26.196639v523.930727c0 14.470573 11.729136 26.198685 26.196638 26.198685h681.111583c14.468526 0 26.198685-11.730159 26.198685-26.198685V614.06763L720.839911 701.264575z"/>
</vector>
ic_delete.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:viewportWidth="1024"
        android:viewportHeight="1024"
        android:width="18dp"
        android:height="18dp">
    <path android:fillColor="#FF666666"
          android:pathData="M154.220431 213.852576c0-24.699542 20.022009-44.723597 44.721551-44.723597l223.61287 0 0-44.723597c0-24.699542 20.022009-44.723597 44.721551-44.723597l89.445148 0c24.699542 0 44.721551 20.024056 44.721551 44.723597l0 44.723597 223.610824 0c24.700565 0 44.723597 20.024056 44.723597 44.723597l0 44.720527L154.220431 258.573103 154.220431 213.852576zM825.055972 348.017228l0 89.446171 0 462.132242c0 24.697495-20.022009 44.720527-44.721551 44.720527L243.664555 944.31617c-24.699542 0-44.721551-20.024056-44.721551-44.720527L198.943005 437.4634l0-89.446171 0-44.720527 626.112967 0L825.055972 348.017228zM377.832278 437.4634c0-24.699542-20.024056-44.723597-44.721551-44.723597-24.699542 0-44.721551 20.024056-44.721551 44.723597l0 372.686071c0 24.699542 20.022009 44.723597 44.721551 44.723597 24.698518 0 44.721551-20.024056 44.721551-44.723597L377.832278 437.4634zM556.722574 437.4634c0-24.699542-20.022009-44.723597-44.723597-44.723597-24.698518 0-44.721551 20.024056-44.721551 44.723597l0 372.686071c0 24.699542 20.024056 44.723597 44.721551 44.723597 24.700565 0 44.723597-20.024056 44.723597-44.723597L556.722574 437.4634zM735.611847 437.4634c0-24.699542-20.022009-44.723597-44.723597-44.723597-24.698518 0-44.721551 20.024056-44.721551 44.723597l0 372.686071c0 24.699542 20.024056 44.723597 44.721551 44.723597 24.700565 0 44.723597-20.024056 44.723597-44.723597L735.611847 437.4634z"/>
</vector>

基本上已經實現了從下面彈起的dialogButton!!!