当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现从底部弹出的Dialog示例(一)

Android实现从底部弹出的Dialog示例(一)

2019年07月24日  | 移动技术网IT编程  | 我要评论

槟榔西施试镜,三国全面战争下载,8l9981

一.概述

先给大家看一下效果图:

点击中间的显示弹框按钮,从底部弹出来一个对话框,用户可以点击拍照或者从相册选择进行相应的操作,下面看看怎么实现。

二.代码实现

主页面布局文件,很简单,一个按钮,响应点击事件:

<?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);
    //将布局设置给dialog
    dialog.setcontentview(inflate);
    //获取当前activity所在的窗体
    window dialogwindow = dialog.getwindow();
    //设置dialog从窗体底部弹出
    dialogwindow.setgravity( gravity.bottom);
    //获得窗体的属性
    windowmanager.layoutparams lp = dialogwindow.getattributes();
    lp.y = 20;//设置dialog距离底部的距离
//    将属性设置给窗体
    dialogwindow.setattributes(lp);
    dialog.show();//显示对话框
  }

  @override
  public void onclick(view view) {
    switch (view.getid()){
      case r.id.takephoto:
        toast.maketext(this,"点击了拍照",toast.length_short).show();
        break;
      case r.id.choosephoto:
        toast.maketext(this,"点击了从相册选择",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以外的区域模糊效果 -->
    <item name="android:backgrounddimenabled">true</item>
    <!-- 无标题 -->
    <item name="android:windownotitle">true</item>
    <!-- 半透明 -->
    <item name="android:windowistranslucent">true</item>
    <!-- dialog进入及退出动画 -->
    <item name="android:windowanimationstyle">@style/actionsheetdialoganimation</item>
  </style>
  <!-- actionsheet进出动画 -->
  <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>

对话框出现动画代码:

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

三.总结

本次实现的dialog主要是通过textview来实现的,并且没有加入状态选择器以及取消按钮,在下篇文章中将对对话框的表现形式稍微进行一下改动。以适应项目中的开发需求。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网