栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一.概述

先给大家看一下效果图:

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

二.代码实现

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



    

接下来看对话框的布局:



  
  
  


根布局为垂直的线性布局,加了一个背景,白色矩形,四个角弧度为5dp,代码如下



  
  


线性布局中是两个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();
  }
}

窗口的样式:

 

    
    @android:color/transparent
    @null
    
    true
    
    @null
    
    true
    
    true
    
    true
    
    @style/ActionSheetDialogAnimation
  
  
  
    @anim/actionsheet_dialog_in
    @anim/actionsheet_dialog_out
  

对话框出现动画代码:


对话框消失的代码:


三.总结

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号