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

所属分类: 软件编程 / Android 阅读数: 47
收藏 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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多