底部彈出框的實(shí)現(xiàn)方式

1. 自定義dialog實(shí)現(xiàn)

/**
     * 顯示支付窗口
     */
    private void showDialog() {
        dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        //填充對(duì)話框的布局
        inflate = LayoutInflater.from(this).inflate(R.layout.item_dialog_pay, null);
        // 初始化控件
        TextView close = (TextView) inflate.findViewById(R.id.tv_supportpay_close);
        LinearLayout weixin = (LinearLayout) inflate.findViewById(R.id.ll_supportpay_weixin);
        LinearLayout zhifubao = (LinearLayout) inflate.findViewById(R.id.ll_supportpay_zhifubao);
        close.setOnClickListener(this);
        weixin.setOnClickListener(this);
        zhifubao.setOnClickListener(this);
        // 將布局設(shè)置給Dialog
        dialog.setContentView(inflate);
        // 獲取當(dāng)前Activity所在的窗體
        Window dialogWindow = dialog.getWindow();
        //最重要的一句話,一定要加上!要不然怎么設(shè)置都不行
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        // 設(shè)置Dialog從窗體底部彈出
        dialogWindow.setGravity(Gravity.BOTTOM);
        // 獲得窗體的屬性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        Display d = dialogWindow.getWindowManager().getDefaultDisplay();
        //獲取屏幕寬
        lp.width = (int) (d.getWidth());
        //寬度按屏幕大小的百分比設(shè)置,這里我設(shè)置的是全屏顯示
        lp.gravity = Gravity.BOTTOM;
        if (lp.gravity == Gravity.BOTTOM)
            lp.y = 20;
        //如果是底部顯示,則距離底部的距離是0
        dialogWindow.setAttributes(lp);
        dialog.show();//顯示對(duì)話框
    }

2. 第三方控件實(shí)現(xiàn)bottomsheet

GitHub地址:https://github.com/Flipboard/bottomsheet

使用方法

  • [ 1 ] 還是先依賴
repositories {
    jcenter()
}

dependencies {
    compile 'com.flipboard:bottomsheet-core:1.5.3'
    compile 'com.flipboard:bottomsheet-commons:1.5.3' // optional
}
  • [ 2 ] 在xml文件中使用
<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_sheet_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:onClick="show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="三" />
    </LinearLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>



布局彈出內(nèi)容

<?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_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF000"
    android:orientation="vertical"
    tools:context="com.itheima.app_bottom.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="條目一" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="條目二" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="條目三" />
</LinearLayout>

  • [ 3 ] 編寫顯示與隱藏邏輯

public void show(View view) {
        //判斷彈出內(nèi)容是否可見
        if (bottomSheetLayout.isSheetShowing()) {
            //可見則隱藏
            bottomSheetLayout.dismissSheet();
        } else {
            if (popView == null) {
                popView = LayoutInflater.from(this).inflate(R.layout.pop_sheet, bottomSheetLayout, false);
            }
            //不可見則顯示 ,對(duì)popView進(jìn)行是否為空判斷可以減少view的創(chuàng)建
            bottomSheetLayout.showWithSheetView(popView);
        }
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,593評(píng)論 25 708
  • 太長(zhǎng)了,還是轉(zhuǎn)載吧...今天在看博客的時(shí)候,無(wú)意中發(fā)現(xiàn)了@Trinea在GitHub上的一個(gè)項(xiàng)目Android開源...
    龐哈哈哈12138閱讀 20,283評(píng)論 3 283
  • 我想我已經(jīng)從茫茫大道中我終于找到了自己的一條路:精益生產(chǎn)。此篇文章為我的第一篇讀精益生產(chǎn)方面的讀書筆記,主要記錄我...
    我在時(shí)光深處等你閱讀 1,676評(píng)論 1 1
  • 約束是一種限制,它通過(guò)對(duì)表的行或列的數(shù)據(jù)做出限制,來(lái)確保表的數(shù)據(jù)的完整性、唯一性。 約束分類 |約束類型 |主...
    lupinwu閱讀 183評(píng)論 0 0
  • 姓名:魏正君《六項(xiàng)精進(jìn)》第270期感謝2組 公司:綿陽(yáng)大北農(nóng)農(nóng)牧科技有限公司 【日精進(jìn)打卡第22天】 【知~學(xué)習(xí)】...
    莫心莫肺閱讀 145評(píng)論 0 0