最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年01月01日 14:36:56   作者:lxn_李小牛  
這篇文章主要介紹了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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

清涧县| 庄浪县| 乌恰县| 延庆县| 宁化县| 余姚市| 江津市| 三门峡市| 滦南县| 长宁区| 元阳县| 杂多县| 江川县| 漾濞| 谢通门县| 海城市| 阿合奇县| 江川县| 新蔡县| 穆棱市| 从化市| 云霄县| 眉山市| 邛崃市| 塔城市| 泸西县| 达拉特旗| 汾西县| 赣榆县| 揭东县| 左云县| 海宁市| 揭阳市| 鄂州市| 肇庆市| 会宁县| 邢台市| 阳曲县| 安平县| 桂平市| 明光市|