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)文章
AndroidStudio接入Unity工程并實現(xiàn)相互跳轉(zhuǎn)的示例代碼
這篇文章主要介紹了AndroidStudio接入Unity工程并實現(xiàn)相互跳轉(zhuǎn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子
這篇文章介紹了Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子,有需要的朋友可以參考一下2013-08-08
Android高級xml布局之輸入框EditText設(shè)計
這篇文章主要為大家詳細(xì)介紹了Android高級xml布局之輸入框EditText設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
ListView異步加載圖片實現(xiàn)思路(優(yōu)化篇)
關(guān)于listview的異步加載,網(wǎng)上其實很多示例了,中心思想都差不多,不過很多版本或是有bug,或是有性能問題有待優(yōu)化,下面就讓在下闡述其原理以探索個中奧秘2013-04-04

