Android實現(xiàn)從底部彈出的Dialog示例(一)
一.概述
先給大家看一下效果圖:

點(diǎn)擊中間的顯示彈框按鈕,從底部彈出來一個對話框,用戶可以點(diǎn)擊拍照或者從相冊選擇進(jìn)行相應(yīng)的操作,下面看看怎么實現(xiàn)。
二.代碼實現(xiàn)
主頁面布局文件,很簡單,一個按鈕,響應(yīng)點(diǎn)擊事件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="com.example.dialogdemo.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="show"
android:text="顯示彈框"
/>
</RelativeLayout>
接下來看對話框的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/background"
android:layout_height="match_parent">
<TextView
android:id="@+id/takePhoto"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="2dp"
android:gravity="center"
android:text="拍照"
android:textColor="#0000ff"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#9e9e9e"
/>
<TextView
android:id="@+id/choosePhoto"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="2dp"
android:gravity="center"
android:text="從相冊選擇"
android:textColor="#0000ff"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
根布局為垂直的線性布局,加了一個背景,白色矩形,四個角弧度為5dp,代碼如下
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff"/> <corners android:radius="5dp"/> </shape>
線性布局中是兩個TextView和一條橫線。也很簡單
下面是java代碼:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private View inflate;
private TextView choosePhoto;
private TextView takePhoto;
private Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void show(View view){
dialog = new Dialog(this,R.style.ActionSheetDialogStyle);
//填充對話框的布局
inflate = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
//初始化控件
choosePhoto = (TextView) inflate.findViewById(R.id.choosePhoto);
takePhoto = (TextView) inflate.findViewById(R.id.takePhoto);
choosePhoto.setOnClickListener(this);
takePhoto.setOnClickListener(this);
//將布局設(shè)置給Dialog
dialog.setContentView(inflate);
//獲取當(dāng)前Activity所在的窗體
Window dialogWindow = dialog.getWindow();
//設(shè)置Dialog從窗體底部彈出
dialogWindow.setGravity( Gravity.BOTTOM);
//獲得窗體的屬性
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.y = 20;//設(shè)置Dialog距離底部的距離
// 將屬性設(shè)置給窗體
dialogWindow.setAttributes(lp);
dialog.show();//顯示對話框
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.takePhoto:
Toast.makeText(this,"點(diǎn)擊了拍照",Toast.LENGTH_SHORT).show();
break;
case R.id.choosePhoto:
Toast.makeText(this,"點(diǎn)擊了從相冊選擇",Toast.LENGTH_SHORT).show();
break;
}
dialog.dismiss();
}
}
窗口的樣式:
<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>
<!-- 無標(biāo)題 -->
<item name="android:windowNoTitle">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- Dialog進(jìn)入及退出動畫 -->
<item name="android:windowAnimationStyle">@style/ActionSheetDialogAnimation</item>
</style>
<!-- ActionSheet進(jìn)出動畫 -->
<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>
對話框出現(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%" />
三.總結(jié)
本次實現(xiàn)的Dialog主要是通過TextView來實現(xiàn)的,并且沒有加入狀態(tài)選擇器以及取消按鈕,在下篇文章中將對對話框的表現(xiàn)形式稍微進(jìn)行一下改動。以適應(yīng)項目中的開發(fā)需求。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 全局Dialog的簡單實現(xiàn)方法
- Android使用Dialog風(fēng)格彈出框的Activity
- Android 中從屏幕左下角彈出Dialog動畫效果的實現(xiàn)代碼
- Android中自定義的dialog中的EditText無法彈出輸入法解決方案
- Android 仿蘋果底部彈出Dialog
- Android 從底部彈出Dialog(橫向滿屏)的實例代碼
- Android解決dialog彈出時無法捕捉Activity的back事件的方法
- Android自定義彈出框dialog效果
- Android 解決dialog彈出時無法捕捉Activity的back事件問題
- Android 8.0如何完美適配全局dialog懸浮窗彈出
相關(guān)文章
Android自定義View實現(xiàn)圓弧進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)圓弧進(jìn)度效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
這篇文章主要為大家詳細(xì)介紹了Android中Handler機(jī)制的使用,文中的示例代碼講解詳細(xì),有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-11-11
Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實現(xiàn)
這篇文章主要介紹了Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Android清空編輯框內(nèi)容功能的實現(xiàn)實例代碼
本篇文章主要介紹了Android清空編輯框數(shù)據(jù)功能的實現(xiàn)實例代碼,非常具有實用價值,需要的朋友可以參考下。2017-03-03
Android webview 遇到android.os.FileUriExposedException錯誤解決辦法
這篇文章主要介紹了Android webview 遇到android.os.FileUriExposedException錯誤解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題解決,需要的朋友可以參考下2017-10-10
android實現(xiàn)人臉識別技術(shù)的示例代碼
本篇文章主要介紹了android人臉識別技術(shù)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03最新評論

