Android實現(xiàn)從底部彈出的Dialog的實例代碼
更新時間:2018年04月09日 11:42:33 作者:拽是男人的本性
這篇文章主要介紹了Android實現(xiàn)從底部彈出的Dialog的實例代碼,非常不錯,具有參考借鑒價值 ,需要的朋友可以參考下
1.點擊按鈕(按鈕的點擊事件在此不在贅述,接下來直接寫底部彈框的實現(xiàn)方式和樣式的設(shè)計)
2.彈框
Dialog dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
//填充對話框的布局
inflate = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
// setCancelable(iscancelable);//點擊外部不可dismiss
//setCanceledOnTouchOutside(isBackCanCelable);
//初始化控件
spinner = (Spinner) inflate.findViewById(R.id.sp);
beizhu = (TextView) inflate.findViewById(R.id.beizhu);
btn_cancel = (Button) inflate.findViewById(R.id.btn_cancel);
btn_ok = (Button) inflate.findViewById(R.id.btn_ok);
//將布局設(shè)置給Dialog
taskProgress.setContentView(inflate);
//獲取當前Activity所在的窗體
Window dialogWindow = taskProgress.getWindow();
//設(shè)置Dialog從窗體底部彈出
dialogWindow.setGravity(Gravity.BOTTOM);
//獲得窗體的屬性
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//如果沒有這行代碼,彈框的內(nèi)容會自適應(yīng),而不會充滿父控件
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.y = 40;//設(shè)置Dialog距離底部的距離
//將屬性設(shè)置給窗體
dialogWindow.setAttributes(lp);
dialog .show();//顯示對話框
在需要消失地方直接
dialog.dismiss();
3.窗口的樣式
<style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog"> <!-- 背景透明 --> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <!-- 浮于Activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 邊框 --> <item name="android:windowFrame">@null</item> <!-- Dialog以外的區(qū)域模糊效果 --> <item name="android:backgroundDimEnabled">true</item> <!-- 無標題 --> <item name="android:windowNoTitle">true</item> <!-- 半透明 --> <item name="android:windowIsTranslucent">true</item> <!-- Dialog進入及退出動畫 --> <item name="android:windowAnimationStyle">@style/ActionSheetDialogAnimation</item> </style> <!-- ActionSheet進出動畫 --> <style name="ActionSheetDialogAnimation" parent="@android:style/Animation.Dialog"> <item name="android:windowEnterAnimation">@anim/actionsheet_dialog_in</item> <item name="android:windowExitAnimation">@anim/actionsheet_dialog_out</item> </style>
4.窗口出現(xiàn)和消失的效果
對話框出現(xiàn)動畫代碼:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200" android:fromYDelta="100%" android:toYDelta="0" />
對話框消失的代碼:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200" android:fromYDelta="0" android:toYDelta="100%" />
5.彈框的整體布局
<?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_task_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/lin_style"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="任務(wù)進度"
android:textSize="17sp" />
<Spinner
android:id="@+id/sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"></Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="備注"
android:textSize="17sp" />
<EditText
android:id="@+id/beizhu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="請輸入備注" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_weight="1"
android:background="@drawable/button_style"
android:minHeight="0dp"
android:minWidth="0dp"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="8dp"
android:text="取消"
android:textColor="#fff" />
<Button
android:id="@+id/btn_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_weight="1"
android:background="@drawable/button_style"
android:minHeight="0dp"
android:minWidth="0dp"
android:paddingBottom="8dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="8dp"
android:text="確定"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
6.lin_style樣式
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp"></corners> <solid android:color="#fff" /> </shape>
7.button_style樣式
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"></corners> <solid android:color="#46b5e9" /> </shape>
6.效果圖

總結(jié)
以上所述是小編給大家介紹的Android實現(xiàn)從底部彈出的Dialog的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:
- Android中自定義的dialog中的EditText無法彈出輸入法解決方案
- Android實現(xiàn)底部對話框BottomDialog彈出實例代碼
- Android實現(xiàn)從底部彈出的Dialog示例(一)
- Android 中從屏幕左下角彈出Dialog動畫效果的實現(xiàn)代碼
- Android 從底部彈出Dialog(橫向滿屏)的實例代碼
- Android使用Dialog風格彈出框的Activity
- android底部彈出iOS7風格對話選項框(QQ對話框)--第三方開源之IOS_Dialog_Library
- Android解決dialog彈出時無法捕捉Activity的back事件的方法
相關(guān)文章
Android使用自定義控件HorizontalScrollView打造史上最簡單的側(cè)滑菜單
側(cè)滑菜單一般都會自定義ViewGroup,然后隱藏菜單欄,當手指滑動時,通過Scroller或者不斷的改變leftMargin等實現(xiàn);多少都有點復雜,完成以后還需要對滑動沖突等進行處理,今天給大家?guī)硪粋€簡單的實現(xiàn),史上最簡單有點夸張,但是的確是我目前遇到過的最簡單的一種實現(xiàn)2016-02-02
Android convinientbanner頂部廣告輪播控件使用詳解
這篇文章主要為大家詳細介紹了Android convinientbanner頂部廣告輪播控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

