国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)從底部彈出的Dialog示例(一)

2019-12-12 04:09:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一.概述

先給大家看一下效果圖:

點(diǎn)擊中間的顯示彈框按鈕,從底部彈出來(lái)一個(gè)對(duì)話框,用戶可以點(diǎn)擊拍照或者從相冊(cè)選擇進(jìn)行相應(yīng)的操作,下面看看怎么實(shí)現(xiàn)。

二.代碼實(shí)現(xiàn)

主頁(yè)面布局文件,很簡(jiǎn)單,一個(gè)按鈕,響應(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>

接下來(lái)看對(duì)話框的布局:

<?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="從相冊(cè)選擇"    android:textColor="#0000ff"    android:textSize="18sp"    android:textStyle="bold" /></LinearLayout>

根布局為垂直的線性布局,加了一個(gè)背景,白色矩形,四個(gè)角弧度為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>

線性布局中是兩個(gè)TextView和一條橫線。也很簡(jiǎn)單

下面是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);    //填充對(duì)話框的布局    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();//顯示對(duì)話框  }  @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)擊了從相冊(cè)選擇",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>    <!-- 無(wú)標(biāo)題 -->    <item name="android:windowNoTitle">true</item>    <!-- 半透明 -->    <item name="android:windowIsTranslucent">true</item>    <!-- Dialog進(jìn)入及退出動(dòng)畫 -->    <item name="android:windowAnimationStyle">@style/ActionSheetDialogAnimation</item>  </style>  <!-- ActionSheet進(jìn)出動(dòng)畫 -->  <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>

對(duì)話框出現(xiàn)動(dòng)畫代碼:

<?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" />

對(duì)話框消失的代碼:

<?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é)

本次實(shí)現(xiàn)的Dialog主要是通過(guò)TextView來(lái)實(shí)現(xiàn)的,并且沒有加入狀態(tài)選擇器以及取消按鈕,在下篇文章中將對(duì)對(duì)話框的表現(xiàn)形式稍微進(jìn)行一下改動(dòng)。以適應(yīng)項(xiàng)目中的開發(fā)需求。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平顶山市| 博白县| 登封市| 宜宾县| 金昌市| 西青区| 马边| 玉山县| 阳城县| 泸西县| 吉林市| 中方县| 安岳县| 申扎县| 安溪县| 雷州市| 象州县| 平利县| 洛浦县| 崇文区| 务川| 清新县| 玉树县| 乌海市| 山东省| 桃园县| 绥芬河市| 修文县| 八宿县| 阳春市| 内乡县| 菏泽市| 河西区| 永嘉县| 萨迦县| 永修县| 台中市| 黄骅市| 青阳县| 儋州市| 普定县|