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

所属分类: 软件编程 / Android 阅读数: 27
收藏 0 赞 0 分享

一.概述

先给大家看一下效果图:

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

二.代码实现

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

<?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来实现的,并且没有加入状态选择器以及取消按钮,在下篇文章中将对对话框的表现形式稍微进行一下改动。以适应项目中的开发需求。

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

更多精彩内容其他人还在看

Android设计登录界面、找回密码、注册功能

这篇文章主要为大家详细介绍了Android设计登录界面的方法,Android实现找回密码、注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android通过手势实现答题器翻页效果

这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android采用双缓冲技术实现画板

这篇文章主要为大家详细介绍了Android采用双缓冲技术实现画板的相关资料,思路清晰,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发之毛玻璃效果实例代码

这篇文章主要给大家分享android开发之毛玻璃效果的实例代码,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Android实现桌面悬浮窗、蒙板效果实例代码

这篇文章主要介绍了Android实现桌面悬浮窗、蒙板效果实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读Android的Volley库的功能结构

这篇文章主要介绍了Android的Volley开发框架的功能结构,Volley是Android开发中网络部分的一大利器,包含很多HTTP协议通信的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中使用Volley库发送HTTP请求的实例教程

这篇文章主要介绍了Android开发中使用Volley库发送HTTP请求的实例教程,包括创建Volley单例的基本知识与取消Request请求的技巧等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿QQ聊天撒花特效 很真实

本文写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android的HTTP操作库Volley的基本使用教程

这篇文章主要介绍了Android的HTTP操作库Volley的基本使用教程,包括JSON请求与图片加载等用法的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿水波纹流量球进度条控制器

这篇文章主要介绍了Android仿水波纹流量球进度条控制器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多