项目中经常会要实现在屏幕底部弹出一个窗口,比如一个分享窗口:

下面详解实现步骤:

1.定义布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:background="@color/white"
  6. android:gravity="center_horizontal"
  7. android:orientation="vertical">
  8.  
  9. <com.piston.usedcar.widget.KiloPiker
  10. android:id="@+id/kp_car_grad"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:descendantFocusability="blocksDescendants"/>
  14.  
  15. <TextView
  16. android:id="@+id/tv_sel_car_grad"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:background="@color/theme_gray_bg"
  20. android:gravity="center"
  21. android:padding="@dimen/common_padding_less"
  22. android:text="确定"
  23. android:textColor="@color/theme_gray_text"
  24. android:textSize="@dimen/text_small"/>
  25. </LinearLayout>

2.定义从底部弹出的动画

  a.弹出动画dialog_enter.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="300"
  4. android:fromYDelta="100%p">
  5. </translate>

  b.退出动画dialog_exit.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="300"
  4. android:toYDelta="100%p">
  5. </translate>

3.在styles.xml定义动画样式

  1. <style name="MyDialogAnimationStyle" parent="android:Animation">
  2. <item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
  3. <item name="@android:windowExitAnimation">@anim/dialog_exit</item>
  4. </style>

4.防止dialog有边距,以便dialog填充整个屏幕宽度,定义如下样式:

  1. <style name="common_dialog" parent="@android:style/Theme.Dialog">
  2. <item name="android:windowBackground">@android:color/transparent</item>
  3. <item name="android:windowNoTitle">true</item>
  4. </style>

5.代码实现:

  1. View view = LayoutInflater.from(AppContext.getContext()).inflate(R.layout.dialog_bottom_car_grad, null);
  2. final Dialog dialog = new Dialog(this, R.style.common_dialog);
  3. dialog.setContentView(view);
  4. dialog.getWindow().setWindowAnimations(R.style.MyDialogAnimationStyle);
  5. dialog.show();
  6.  
  7. final KiloPiker kpCarGrad = (KiloPiker) view.findViewById(R.id.kp_car_grad);
  8. TextView mViewPengyou = (TextView) view.findViewById(R.id.tv_sel_car_grad);
  9.  
  10. kpCarGrad.setNumberPickerDividerColor(kpCarGrad, R.color.background_split);
  11.  
  12. kpCarGrad.setDisplayedValues(CAR_GRAD_ARRAY);
  13. kpCarGrad.setMaxValue(CAR_GRAD_ARRAY.length - 1);
  14. kpCarGrad.setMinValue(0);
  15.  
  16. kpCarGrad.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
  17. @Override
  18. public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  19. carGradSelPos = newVal;
  20. }
  21. });
  22.  
  23. mViewPengyou.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. if (carGradSelPos == 0) {
  27. tvSynthCarGrad.setText("优秀车况");
  28. } else if (carGradSelPos == 1) {
  29. tvSynthCarGrad.setText("良好车况");
  30. }
  31. dialog.dismiss();
  32. }
  33. });
  34.  
  35. // 设置相关位置,一定要在 show()之后
  36. Window window = dialog.getWindow();
  37. window.getDecorView().setPadding(0, 0, 0, 0);
  38. WindowManager.LayoutParams params = window.getAttributes();
  39. params.width = WindowManager.LayoutParams.MATCH_PARENT;
  40. params.gravity = Gravity.BOTTOM;
  41. window.setAttributes(params);

  

底部菜单实现(Dialog方案)的更多相关文章

  1. Vue 在手机上键盘把底部菜单顶上去的解决方案

    Vue 在手机上键盘把底部菜单顶上去的解决方案 ios和安卓的键盘的区别 ios和安卓的键盘的区别弹起方式不同, ios直接弹出键盘, 不影响页面, 而安卓键盘弹起时会把页面顶起来, 这样就会把底部菜 ...

  2. Android 底部弹出Dialog(横向满屏)

    项目中经常需要底部弹出框,这里我整理一下其中我用的比较顺手的一个方式(底部弹出一个横向满屏的dialog). 效果图如下所示(只显示关键部分): 步骤如下所示: 1.定义一个dialog的布局(lay ...

  3. Android底部菜单的实现

    前言:以前制作菜单使用TabHost,但是android 3.0以上就被废弃了,google已经不建议使这个类了.ActionBar也是菜单,不过在头部,算是导航了 ===本文就介绍怎么制作底部菜单= ...

  4. 转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

    http://www.cnblogs.com/lichenwei/p/3985121.html 记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果, ...

  5. 转-TabHost组件(一)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...

  6. 转-TabHost组件(二)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3975095.html 上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定 ...

  7. Android应用主界面底部菜单实现

    介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的  <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...

  8. Android自定义控件系列(四)—底部菜单(下)

    转载请注明出处:http://www.cnblogs.com/landptf/p/6290862.html 在app中经常会用到底部菜单的控件,每次都需要写好多代码,今天我们用到了前几篇博客里的控件来 ...

  9. Android中软键盘弹出时底部菜单上移问题

    当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncrea ...

  10. [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单

    Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...

随机推荐

  1. Java基础复习--java.util.Timer定时任务

    在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下. (1)sche ...

  2. ASIHttprequest-创建同步请求

    ASIHttprequest-创建同步请求 当你发送一个同步请求后,该请求会在当前的应用主线程中运行并获取程序的控制权限,也就说你的程序处于锁定的状态,在这个期间,你进行不了任何的操作,直到该请求返回 ...

  3. Java设计模式_创建型模式_单例模式

    单例模式的实现: 定义一个类,在类中定义该类的静态变量,再定一个一个获取该类的静态变量的方法. UML图:

  4. urllib url解析学习

    #!/usr/bin/env python # encoding: utf-8 from urllib.parse import * #urlparse:解析url分段 #urlsplit:类似url ...

  5. TOTP:Time-based One-time Password Algorithm

    转自: http://www.cnblogs.com/dyingbleed/archive/2012/12/05/2803782.html http://en.wikipedia.org/wiki/T ...

  6. ipv6nginx错误

    400 Bad Request The plain HTTP request was sent to HTTPS port错误参考官方文档解决方法如下: server {listen 80;liste ...

  7. 10.OpenStack块存储服务

    添加块存储服务 安装和配置控制器节点 创建数据库 mysql -uroot -ptoyo123 CREATE DATABASE cinder; GRANT ALL PRIVILEGES ON cind ...

  8. 【linux高级程序设计】(第十二章)Linux多线程编程 2

    线程同步机制 互斥锁通信机制 int pthread_mutex_init (pthread_mutex_t *__mutex, __const pthread_mutexattr_t *__mute ...

  9. Selenium2+python自动化4-Pycharm使用【转载】

    前言 在写脚本之前,先要找个顺手的写脚本工具.python是一门解释性编程语言,所以一般把写python的工具叫解释器.写python脚本的工具很多,小编这里就不一一列举的,只要自己用着顺手就可以的, ...

  10. poj 1375(解析几何)

    Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4292   Accepted: 1288 Descrip ...