Android 從底部彈出Dialog(橫向滿屏)的實例代碼
項目中經(jīng)常需要底部彈出框,這里我整理一下其中我用的比較順手的一個方式(底部彈出一個橫向滿屏的dialog)。
效果圖如下所示(只顯示關(guān)鍵部分):

步驟如下所示:
1.定義一個dialog的布局(lay_share.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="match_parent" android:background="@color/white" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:paddingBottom="@dimen/padding_15" android:paddingTop="@dimen/padding_15"> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="@dimen/padding_5" android:drawableTop="@mipmap/ic_weixin_share" android:gravity="center" android:text="微信" android:textColor="@color/color_999999" android:textSize="@dimen/text_14" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="@dimen/padding_5" android:drawableTop="@mipmap/ic_circle_share" android:gravity="center" android:text="朋友圈" android:textColor="@color/color_999999" android:textSize="@dimen/text_14" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="@dimen/padding_5" android:drawableTop="@mipmap/ic_weibo_share" android:gravity="center" android:text="微博" android:textColor="@color/color_999999" android:textSize="@dimen/text_14" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="@dimen/padding_10" android:layout_marginRight="@dimen/padding_10" android:background="@color/color_c9c9c9" /> <TextView android:id="@+id/tv_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="@dimen/padding_15" android:text="取消" android:textColor="@color/color_666666" android:textSize="@dimen/text_18" /> </LinearLayout>
2.定義彈出框彈出動畫(dialog_enter.xml)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromYDelta="100%p" android:toYDelta="0" /> </set> dialog_enter.xml
3.定義彈出框隱藏動畫(dialog_exit.xml)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromYDelta="0" android:toYDelta="100%p" /> </set> dialog_exit.xml
4.定義動畫style
<!--彈出框動畫--> <style name="share_animation" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/dialog_enter</item> <item name="android:windowExitAnimation">@anim/dialog_exit</item> </style>
5.定義對話框樣式
<!-- 對話框樣式 --> <style name="dialog_bottom_full" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:scrollHorizontally">true</item> </style>
6.最后,在需要從底部彈出dialog的地方,直接調(diào)用showDialog()方法
/**
* 顯示分享彈出框
*/
private void showDialog() {
if (mShareDialog == null) {
initShareDialog();
}
mShareDialog.show();
}
/**
* 初始化分享彈出框
*/
private void initShareDialog() {
mShareDialog = new Dialog(this, R.style.dialog_bottom_full);
mShareDialog.setCanceledOnTouchOutside(true);
mShareDialog.setCancelable(true);
Window window = mShareDialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.share_animation);
View view = View.inflate(this, R.layout.lay_share, null);
view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mShareDialog != null && mShareDialog.isShowing()) {
mShareDialog.dismiss();
}
}
});
window.setContentView(view);
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//設(shè)置橫向全屏
}
以上所述是小編給大家介紹的Android 從底部彈出Dialog(橫向滿屏)的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android 全局Dialog的簡單實現(xiàn)方法
- Android使用Dialog風格彈出框的Activity
- Android實現(xiàn)從底部彈出的Dialog示例(一)
- Android 中從屏幕左下角彈出Dialog動畫效果的實現(xiàn)代碼
- Android中自定義的dialog中的EditText無法彈出輸入法解決方案
- Android 仿蘋果底部彈出Dialog
- Android解決dialog彈出時無法捕捉Activity的back事件的方法
- Android自定義彈出框dialog效果
- Android 解決dialog彈出時無法捕捉Activity的back事件問題
- Android 8.0如何完美適配全局dialog懸浮窗彈出
相關(guān)文章
淺談Android為RecyclerView增加監(jiān)聽以及數(shù)據(jù)混亂的小坑
下面小編就為大家?guī)硪黄獪\談Android為RecyclerView增加監(jiān)聽以及數(shù)據(jù)混亂的小坑。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Android?registerForActivityResult新用法實現(xiàn)兩個Activity間數(shù)據(jù)傳遞
這篇文章主要為大家介紹了Android?registerForActivityResult新用法實現(xiàn)兩個Activity間數(shù)據(jù)傳遞詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
Android使用ViewFlipper實現(xiàn)圖片上下自動輪播的示例代碼
這篇文章主要介紹了Android使用ViewFlipper實現(xiàn)圖片上下自動輪播的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2021-05-05
Android Studio如何查看源碼并調(diào)試的方法步驟
這篇文章主要介紹了Android Studio如何查看源碼并調(diào)試的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-05-05
Flutter實現(xiàn)Android滾動懸浮效果過程
這篇文章主要介紹了Flutter實現(xiàn)Android滾動懸浮效果,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習吧2023-01-01
Flutter開發(fā)之設(shè)置應(yīng)用名稱及圖標的教程
這篇文章主要介紹了Flutter設(shè)置應(yīng)用名稱及圖標的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android編程實現(xiàn)輸入框動態(tài)自動提示功能
這篇文章主要介紹了Android編程實現(xiàn)輸入框動態(tài)自動提示功能,結(jié)合實例形式分析了AutoCompleteTextView相關(guān)使用技巧,需要的朋友可以參考下2017-03-03
Android 解決使用SearchView時軟鍵盤不支持actionSearch的問題
本文主要介紹使用SearchView時軟鍵盤不支持actionSearch,這里提供了解決方案,希望能幫助開發(fā)Android應(yīng)用的同學(xué)2016-07-07
Android使用CountDownTimer實現(xiàn)倒計時效果
這篇文章主要為大家詳細介紹了Android使用CountDownTimer實現(xiàn)倒計時效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09

